将txt文件与Android中的输入字符串进行比较

时间:2013-11-16 06:33:09

标签: java android file-io

我创建了一个txt文件并将其保存在data/data/com.xxx/files/mynote/txt的文件资源管理器中,当我在编辑文本中输入一个字符串并单击按钮时。

我想比较txt文件内容和输入字符串。在我的txt文件中,只存储了最小长度5到6。我该怎么做呢?我已经尝试但它无法正确比较 - 当我输入相同的字符串时,它会转到else块部分,因为我保存在我的txt文件中,例如" tazin"。

这是我的代码。

  btnSubmitCode=(Button)findViewById(R.id.button_Submit);
        btnSubmitCode.setOnClickListener(new OnClickListener() {

        @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

            if(editText_Code.getText().toString().isEmpty()) 
                {
                    Toast.makeText(getApplicationContext(),"Please Enter Code", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    String strCode = editText_Code.getText().toString().trim();
                    create_File(strCode);
                    readFile();
                }

                String temp;
                String searchString = "tazin";

                try {
                    File file=new File("/data/data/com.xxxxx/files/mynote.txt/");

                    BufferedReader in = new BufferedReader(new FileReader(file));
                    while (in.readLine() != null)
                    {
                        temp = in.readLine();
                        System.out.println("String from txt file ="+temp);
                        if(searchString.equals(temp))
                        {
                            System.out.println("word is match");
                            in.close();
                            return;
                        }
                        else
                        {
                            System.out.println("word is not match");
                        }
                    }
                    in.close();

                } 

                 catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });




//Write File
    private void create_File(String strtext)
    {
        FileOutputStream fos = null;
        try
        {
            fos = openFileOutput("mynote.txt", MODE_PRIVATE);
            fos.write(strtext.getBytes());
            Toast.makeText(getApplicationContext(), "File created succesfully",
                    Toast.LENGTH_SHORT).show();

        }
        catch(FileNotFoundException fexception)
        {
            fexception.printStackTrace();

        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }

        finally
        {
            if (fos != null) 
            {
                try 
                {
                    // drain the stream
                    fos.flush();
                    fos.close();
                }
                catch (IOException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }


    //Read File

    private void readFile()
    {


        FileInputStream fis;

        try 
        {
            fis = openFileInput("mynote.txt");
            byte[] reader = new byte[fis.available()];
            while (fis.read(reader) != -1) 
            {

            }


            textView_ReadCode.setText(new String(reader));
            strReadFile = new String(reader);
            System.out.println("Read File Into String Format " + strReadFile);


            Toast.makeText(getApplicationContext(), "File read succesfully",
                    Toast.LENGTH_SHORT).show();

            if (fis != null)
            {
                fis.close();
            }
        } 
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            Log.e("Read File", e.getLocalizedMessage());
        }

    }

2 个答案:

答案 0 :(得分:1)

您滥用BufferedReader.readLine()方法。你每次通过while循环调用它两次;一次在while条件中,一次在循环块本身。因此,当你执行temp = in.readLine()然后执行searchString.equals(temp)比较时,你已经跳过了文本文件中的一行。像这样构建你的循环:

temp = in.readLine();
while (temp != null)
{
    System.out.println("String from txt file =" + temp);

if(searchString.equals(temp))
{
        System.out.println("word is match");
        in.close();
        return;
    }
    else
    {
        System.out.println("word is not match");
    }

    temp = in.readLine();
}

修改

根据我们在评论中的聊天,这里是您需要的更新代码:

btnSubmitCode.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            String str = editText_Code.getText().toString();

            if(str == null || str.equals(""))
            {
                Toast.makeText(getApplicationContext(), "Please Enter Code", Toast.LENGTH_SHORT).show();
            }
            else
            {
                String strCode = str.trim();
                if(checkCode(strCode))
                {
                    System.out.println("word is match");   
                }
                else
                {
                    System.out.println("word is not match");                    
                }
            }
        }
    }
);
...
...

// Returns true if the code has already been used
// Returns false if the code is new
// Creates mynote.txt if it doesn't exist
// Appends code if it is new
private boolean checkCode(String code)
{
    String temp;
    File file = new File(getFilesDir() + "mynote.txt");

    if(file.exists())
    {
        try
        {
            BufferedReader in = new BufferedReader(new FileReader(file));
            temp = in.readLine();
            while (temp != null)
            {
                if(code.equals(temp))
                {
                    // Match was found
                    // Clean up and return true
                    in.close();
                    return true;
                }

                temp = in.readLine();
            }
            in.close();
        }
        catch (FileNotFoundException e)
        {

        }
        catch (IOException e)
        {

        }

    }

    // Match was not found or File doesn't exist
    // Append code to mynote.txt and return false
    try
    {
        BufferedWriter out = new BufferedWriter(new FileWriter(file, true));
        out.newLine();
        out.write(code);
        out.close();
    }
    catch (IOException e)
    {

    }

    return false;
}

答案 1 :(得分:0)

在你的问题中,你写了不同的路径然后你写了代码,所以只需检查你的文件路径。