Android-将字符串与原始文件夹中的.txt文件进行比较

Android-将字符串与原始文件夹中的.txt文件进行比较,android,string,compare,Android,String,Compare,我想知道如何将字符串值与.txt文件的每一行进行比较,得到相等的值 我从.txt文件中获取所有值,但我不知道如何比较它。 比如说 ABC CBA CCC 在我的.txt文件中, 在我的活动中 String someText = "ABC"; 以及如何将其与.txt文件eacline进行比较。 我完成了下面的代码来获取.txt文件值 String result; try { Resources res = getResources();

我想知道如何将字符串值与.txt文件的每一行进行比较,得到相等的值

我从.txt文件中获取所有值,但我不知道如何比较它。 比如说

ABC
CBA
CCC
在我的.txt文件中, 在我的活动中

String someText = "ABC";
以及如何将其与.txt文件eacline进行比较。 我完成了下面的代码来获取.txt文件值

String result;
        try {
            Resources res = getResources();
            InputStream in_s = res.openRawResource(R.raw.out);

            byte[] b = new byte[in_s.available()];
            in_s.read(b);
            result = new String(b);
            tx.setText(result);
        } catch (Exception e) {
            // e.printStackTrace();
            result = "Error: can't show file.";
            tx.setText(result);
        }

我认为您的问题在于您读取文件的方式。
您当前将所有文件内容读入一个字符串中,这使得您很难进行比较。
好的,现在是程序:

  • 打开文件(创建InputStream,使用断言,然后将其包装在BufferedReader中)
  • 逐行读取,将值存储在变量中(使用bufferreader的readline()函数)
  • 为此变量和字符串(string.equal)调用比较字符串函数
  • 我希望你能明白。所有剩下的任务都是关于Android文档的

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(
                new InputStreamReader(getAssets().open("out.txt"), "UTF-8")); 
    
            // do reading, usually loop until end of file reading 
            String mLine = reader.readLine();
            while (mLine != null) {
               //process line              
               //mLine = reader.readLine(); 
               if ("ABC".equals(mLine)){
                   Toast.makeText(this, "Yuppppiiiiii", 1000).show();                  
               }
               mLine = reader.readLine();
            }
        } catch (IOException e) {
            //log the exception
        } finally {
            if (reader != null) {
                 try {
                     reader.close();
                 } catch (IOException e) {
                     //log the exception
                 }
            }
        }