简体   繁体   English

无法打开文本文件

[英]Unable to open the text file

I have a text file of dictionary and I want to match words with EditText value. 我有一个字典的文本文件,我想将单词与EditText值匹配。 But I am getting the following error java.io.FileNotFoundException: /Test/res/raw/test.txt: open failed: ENOENT (No such file or directory) 但是我收到以下错误java.io.FileNotFoundException: /Test/res/raw/test.txt: open failed: ENOENT (No such file or directory)

This what I have done so far. 到目前为止,这是我所做的。

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();
      }    
}

Thanks in advance. 提前致谢。

You need to use an input stream to open and read your file 您需要使用输入流来打开和读取文件

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

OR 要么

seeing as you have made a Raw folder within your res folder, call the following getResources().openRawResources(myResourceName) from within the activity. 看到您在res文件夹中创建了Raw文件夹后,请在活动中调用以下getResources().openRawResources(myResourceName)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM