【问题标题】:how to store data of text file into a string如何将文本文件的数据存储到字符串中
【发布时间】:2016-12-24 13:27:57
【问题描述】:

我的文本文件名 pass 有文本 1234 但是当我从文件中选择这个文本并与字符串 epass 比较时,它也是 1234 使用下面的代码无法匹配它。为什么有两个字符串不相等?

try {
    InputStream fr = getResources().openRawResource(R.raw.pass);
    BufferedReader br = new BufferedReader(new InputStreamReader(fr));
    String s=br.readLine().toString().trim();
    if(epass.equals(s))
    {
        t.setText("");

        Intent main= new Intent(getApplicationContext(),MainActivity.class);

        startActivity(main);
        this.finish();
    }
    else
    {
        show.setText("Wrong Passcode");
        show.setTextColor(Color.RED);
        t.setText("");
        epass="";
    }
    epass="";
}
catch(IOException ex)
{

}

【问题讨论】:

    标签: string-comparison file-handling


    【解决方案1】:

    如果在String s=br.readLine().toString().trim();之后插入下面的代码,输出是什么?

        System.out.println("'" + s + "'");
        System.out.println("'" + epass + "'");
    

    可能是行尾字符或类似字符有问题

    【讨论】:

    • 12-24 18:38:04.474 14976-14976/com.example.adnaninayat.myapplication I/System.out: '1234' 12-24 18:38:04.474 14976-14976/com。 example.adnaninayat.myapplication I/System.out: '1234'
    • 很奇怪..如果你这样比较会发生什么:Integer.valueOf(s) == Integer.valueOf(epass)
    • 很多错误当我写 Integer.valueOf(s) == Integer.valueOf(epass)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 2010-11-01
    相关资源
    最近更新 更多