1

I have a text file of dictionary and I want to match words with EditText value. But I am getting the following error 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.

Steve P.
  • 14,489
  • 8
  • 42
  • 72
user2380151
  • 151
  • 1
  • 1
  • 10

2 Answers2

0

You need to use an input stream to open and read your file

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

OR

seeing as you have made a Raw folder within your res folder, call the following getResources().openRawResources(myResourceName) from within the activity.

gavlaaaaaaaa
  • 612
  • 2
  • 7
  • 11
0

If the file is on the external storage, you should add 'READ_EXTERNAL_STORAGE' permission to your manifest.

Michael Cheremuhin
  • 1,381
  • 11
  • 17