merlin’s girl 2017-04-03 04:37 采纳率: 20%
浏览 1266
已采纳

android实验中,用文件存储数据,如果重复就不写入,但一直没成功,不知道哪里错了

代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tianjia);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar1);
toolbar.setTitle("添加");
toolbar.inflateMenu(R.menu.menu2);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Intent intent=new Intent();
Bundle bundle=new Bundle();
name=(EditText)findViewById(R.id.name);
String name1=name.getText().toString();
phone=(EditText)findViewById(R.id.phone);
String phone1=phone.getText().toString();
FileInputStream in;
BufferedReader read = null;
String s1="",s2="";
try {

               in = openFileInput("date5");
               read = new BufferedReader(new InputStreamReader(in));
               String line = "";
               while ((line = read.readLine()) != null) {
                   StringTokenizer st = new StringTokenizer(line, ";"); //设置数据之间 的分隔符为;
                    s1 = st.nextToken();//读取的时候是 String 类
                    s2 = st.nextToken();
                   if (s1== name1 || s2 == phone1 ) {
                       Toast.makeText(tianjia.this, "该快捷拨号已存在", Toast.LENGTH_SHORT).show();
                       //AlertDialog alertDialog=new AlertDialog();
                       break;
                   }
               }
           }catch (IOException e){
               e.printStackTrace();
           }finally {
               try {
                   if (read != null) {
                       read.close();
                   }
               }catch(IOException e){
                   e.printStackTrace();
               }
           }
            if(s1!= name1&&s2 != phone1&&!TextUtils.isEmpty(name1)&&!TextUtils.isEmpty(phone1))
            {
                bundle.putString("name1",name1);
                bundle.putString("phone1",phone1);
                intent.putExtras(bundle);
                setResult(RESULT_OK,intent);
                finish();
            }
            return false;
        }
    });


}
@Override
protected void onDestroy() {
    if(name!=null) {
        String Input1 = name.getText().toString();
        String Input2 = phone.getText().toString();
        if(!TextUtils.isEmpty(Input1)&&!TextUtils.isEmpty(Input2)) save(Input1, Input2);
    }
    super.onDestroy();
}
public void save(String i1,String i2){
    FileOutputStream out;
    BufferedWriter writer=null;
    FileInputStream in;
    BufferedReader read=null;
    String s1=null,s2=null;
    try{
        out=openFileOutput("date5", Context.MODE_APPEND);
        in=openFileInput("date5");
        writer=new BufferedWriter(new OutputStreamWriter(out));
        read = new BufferedReader(new InputStreamReader(in));
        String line="";
        while((line=read.readLine())!=null){
            StringTokenizer st = new StringTokenizer(line, ";"); //设置数据之间 的分隔符为;
             s1 = st.nextToken();//读取的时候是 String 类
             s2 = st.nextToken();
            if(s1==i1||s2==i2) {
                Toast.makeText(tianjia.this, "该快捷拨号已存在", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        if(s1!=i1&&s2!=i2&&!TextUtils.isEmpty(i1)&&!TextUtils.isEmpty(i2)) {
            writer.write(i1);
            writer.write(";");
            writer.write(i2);
            writer.write(";");
            writer.newLine();
        }
    }catch(IOException e){
        e.printStackTrace();
    }finally {
        try {
            if (writer != null) {
                writer.close();
            }
        }catch(IOException e){
                e.printStackTrace();
            }
    }
}

}
Toast标签一直没显示,说明判断是否相等处错了,但不知道为什么,求解!!!!!

  • 写回答

3条回答 默认 最新

  • merlin’s girl 2017-04-03 05:43
    关注

    问题已解决。判断字符串是否相等地方变成String.equals(String)就可以了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波