1

我有一个字典文本文件,我想用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();
      }    
}

提前致谢。

4

2 回答 2

0

如果文件在外部存储上,您应该在清单中添加“READ_EXTERNAL_STORAGE”权限。

于 2013-06-10T08:12:43.603 回答
0

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

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

或者

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

于 2013-06-10T08:08:07.740 回答