从textfile读取值并与edittext进行比较

时间:2015-08-17 10:39:05

标签: android file-handling word-list

我想比较编辑文本的值与文件中的所有行。(文件是单词列表)。

我做到了,

        try{
            final InputStream file = getAssets().open("words.txt");
            reader = new BufferedReader(new InputStreamReader(file));
            String line = reader.readLine();
            while(line != null){

                line = reader.readLine();
                if(ss == line.toLowerCase()){
                    Toast.makeText(this, "Working!", Toast.LENGTH_SHORT)
                            .show();
                }
                else{
                    Toast.makeText(this, "Not found !", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        } catch(IOException ioe){
            ioe.printStackTrace();
        }

这里ss是textfield的值。

 String ss = (res.getText()).toString();

这是words.txt文件 https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt

但上面的代码无效。

编辑:

我已经检查过文件是否打开,问题是文件没有打开。

        try{
            final InputStream file = getAssets().open("words.txt");
            Toast.makeText(this, "File Opened", Toast.LENGTH_SHORT)
                    .show();
            reader = new BufferedReader(new InputStreamReader(file));
            String line ;
            while((line = reader.readLine()) != null){

                if(ss.equalsIgnoreCase(line)){
                    Toast.makeText(this, "Working!", Toast.LENGTH_SHORT)
                            .show();
                    TextView tv = myTextViewList.get(counter);
                    tv.setText(line);
                }
                else{
                    Toast.makeText(this, "Not found !", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        } catch(IOException ioe){
            ioe.printStackTrace();
        }

4 个答案:

答案 0 :(得分:0)

试试这个

ss.equalsIgnoreCase(line.toLowerCase())

替换" =="使用" equalsIgnoreCase"或" equals"

答案 1 :(得分:0)

您正在读取该行两次,并使用equalsIgnoreCase来比较字符串

        while((line = reader.readLine()) != null){

            if(ss.equalsIgnoreCase(line)){
                Toast.makeText(this, "Working!", Toast.LENGTH_SHORT)
                        .show();
            }
            else{
                Toast.makeText(this, "Not found !", Toast.LENGTH_SHORT)
                        .show();
            }
        }

答案 2 :(得分:0)

试试这个...

BufferedReader in = new BufferedReader(new FileReader(filepath));
boolean found = false;
while (( line = in.readLine()) != null)
{
if (line.contains(str))
{
    found = true;
    break; //break out of loop now
}
 }
 in.close();

if (found)
{
    System.out.println("Yes");
}
else
{
  System.out.println("No");
BufferedWriter out = new BufferedWriter(new FileWriter(filepath,true));
out.newLine();
out.write(str);
out.close();
}

答案 3 :(得分:0)

在你的代码中,它会逐行比较直到它到达你的最后一行(即235886)并且它会在循环中连续运行。 它一个接一个地检查所有行,所以当你第一次比较并且如果它成功然后打破循环, 并且还使用.equals进行字符串比较而不是'=='

String ss = "a";
        try{
            final InputStream file = getAssets().open("words.txt");
            BufferedReader reader = new BufferedReader(new InputStreamReader(file));
            String line = reader.readLine();
            while(line != null){

                line = reader.readLine();
                if(ss.equals(line)){
                    Toast.makeText(this, "Working!", Toast.LENGTH_SHORT)
                            .show();
                    break;
                }
                else{
                    Toast.makeText(this, "Not found !", Toast.LENGTH_SHORT)
                            .show();
                    break;
                }
            }
        } catch(IOException ioe){
            ioe.printStackTrace();
        } 

希望这会对你有帮助..