1

I am actually trying to search for given string in the text file which I have stored in the assets folder inside my Android application. The code that I have written is:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button;
    final EditText obedittext;

    button =(Button)findViewById(R.id.button1);
    obedittext =(EditText)findViewById(R.id.editText1);


    button.setOnClickListener(
            new View.OnClickListener()
            {
                boolean textfound;
                public void onClick(View view)
                {
                    textfound = searchtext(obedittext.getText().toString());
                    if(textfound)
                        maketoast(obedittext.getText().toString());
                    else
                        maketoast("Unsuccessfull");
                }
    });

}

protected boolean searchtext(String string) {
    // TODO Auto-generated method stub


    BufferedReader br = null;
    try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader("mneumo.txt"));

        while ((sCurrentLine = br.readLine()) != null) {
            if(sCurrentLine.equals(string)) {
                return true;
            }
        }
        br.close();


    } catch (IOException e) {
        e.printStackTrace();
    }
        finally{

        }
    return false;
}


private void maketoast(String string) {
    // TODO Auto-generated method stub

    Context context = getApplicationContext();

    Toast toast = Toast.makeText(context, string , Toast.LENGTH_SHORT);
    toast.show();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

the error which I received was:

03-06 01:17:01.330: W/System.err(1170): java.io.FileNotFoundException: /mneumo.txt: open failed: ENOENT (No such file or directory)
03-06 01:17:01.330: W/System.err(1170):     at libcore.io.IoBridge.open(IoBridge.java:409)
03-06 01:17:01.330: W/System.err(1170):     at java.io.FileInputStream.<init>(FileInputStream.java:78)
03-06 01:17:01.340: W/System.err(1170):     at java.io.FileInputStream.<init>(FileInputStream.java:105)
03-06 01:17:01.340: W/System.err(1170):     at java.io.FileReader.<init>(FileReader.java:66)
03-06 01:17:01.340: W/System.err(1170):     at com.example.demo.MainActivity.searchtext(MainActivity.java:60)
03-06 01:17:01.340: W/System.err(1170):     at com.example.demo.MainActivity$1.onClick(MainActivity.java:41)

The sample file is,

SPINAL ANESTHESIA AGENTS
XYLOCAINE: WHERE NOT TO USE WITH EPINEPHRINE
GENERAL ANAESTHESIA: EQUIPMENT CHECK PRIOR TO INDUCING

If the string is found, it is supposed to show a toast with that string. But it always says "file not found". And I am total newbie.

This is for an app that is similar to a dictionary.

I did refer to other questions in this site but I still can't figure out what the problem is. Should I use assetmanager or something else?

Simon W
  • 5,481
  • 3
  • 24
  • 35
arvind
  • 778
  • 1
  • 9
  • 16

2 Answers2

1

Your code statement

br = new BufferedReader(new FileReader("mneumo.txt"));

refers to the wrong location. Hence, it can't read the file. Instead, replace your above line by following

br = new BufferedReader(new InputStreamReader(getAssets().open("mneumo.txt")));

This way, your file should be found and opened. Files stored in the app's asset folder should always read that way and not via hard-coded directory strings or app-relative paths.

Danny
  • 26
  • 2
0

You want to read from assets, right? You need to use AssetManager to pull that off. See below:

    InputStream is;
    try {
        is = getAssets().open("myfile.txt");
        byte[] buffer = new byte[is.available()];
        is.read(buffer);
        is.close();
        Toast.makeText(this, new String(buffer, "UTF-8"), Toast.LENGTH_SHORT).show();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
Matter Cat
  • 1,538
  • 1
  • 14
  • 23