无法打开文本文件

时间:2013-06-10 08:00:41

标签: android eclipse

我有一个字典的文本文件,我想匹配EditText值的单词。但是我收到以下错误java.io.FileNotFoundException: /Test/res/raw/test.txt: open failed: ENOENT (No such file or directory)

这是我到目前为止所做的。

public void show(View view) throws IOException
{

    File file = new File("/Test/res/raw/test.txt");
    if (!file.exists())
    {
         helloTxt1.setText("empty");    
    }

    try 
    {  
        FileInputStream in = new FileInputStream(file);
        int len = 0;
        byte[] data1 = new byte[1024];

        while ( -1 != (len = in.read(data1)) )
        {
            if(new String(data1, 0, len).contains(edittxt.getText().toString()))
            {
                  helloTxt1.setText("1");
            }
            else
            {
                  helloTxt1.setText("0");
            }                             
        }
    } catch (FileNotFoundException e) 
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }    
}

提前致谢。

2 个答案:

答案 0 :(得分:0)

您需要使用输入流来打开和阅读您的文件

InputStream inputStream = context.getAssets().open("test.txt");并使用Reader

OR

看到您在res文件夹中创建了一个Raw文件夹,请从活动中调用以下getResources().openRawResources(myResourceName)

答案 1 :(得分:0)

如果文件位于外部存储器上,则应向清单添加“READ_EXTERNAL_STORAGE”权限。

相关问题