I am creating a Dictionary application. There's a popular button around the application that enables customers to:

  • short-click to include the presently-seen word in to the Favourite list
  • lengthy-click to see the widely used list (of added words).

To date, I've coded the following:

Up-to-date CODE:

//Writing lines to myFavourite.txt
    btnAddFavourite = (ImageButton) findViewById(R.id.btnAddFavourite);
    btnAddFavourite.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // Writing the content
                try {
                // opening myFavourite.txt for writing
                  OutputStreamWriter out = new OutputStreamWriter(openFileOutput("myFavourite.txt", MODE_APPEND));
                // writing the ID of the added word to the file
                  out.write(mCurrentWord);  
                // closing the file
                  out.close();
                } catch (java.io.IOException e) {
                  //doing something if an IOException occurs.
                }
                Toast toast = Toast.makeText(ContentView.this, R.string.messageWordAddedToFarvourite, Toast.LENGTH_SHORT);
                toast.show();
            }
        });

    //Reading lines from myFavourite.txt
    btnAddFavourite.setOnLongClickListener(new View.OnLongClickListener() {         

            @Override
            public boolean onLongClick(View v) {

             //trying opening the myFavourite.txt
               try {
             // opening the file for reading
                InputStream instream = openFileInput("myFavourite.txt");

             // if file the available for reading
                if (instream != null) {
            // prepare the file for reading
                InputStreamReader inputreader = new InputStreamReader(instream);
                BufferedReader buffreader = new BufferedReader(inputreader);

            String line;

            // reading every line of the file into the line-variable, on line at the time
            try {
                while ((line = buffreader.readLine()) != null) {
                  // do something with the settings from the file
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

          }

          // closing the file again
          try {
            instream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        } catch (java.io.FileNotFoundException e) {

          // ding something if the myFavourite.txt does not exits
        }
        return false;

        }});
        }

However, the widely used button doesn't work using the above code lines.

The file myFavourite.txt does exits (in data/data/my_project/files in Eclipse) however it only consists of one lately added word. In addition, the application is pressure-closed once the Favourite button is lengthy-clicked on.

What have I been doing wrong? I'm very grateful if everyone might help me solve this issue. Thanks greatly.

========

EDIT

Thanks greatly for the help. I up-to-date my code to mirror your comments and prompts. Right now, there has been some enhancements: The Widely Used words happen to be written towards the file myFavourite.txt like word2 word2 word3... (though I would like these to come in new lines).

However, the widely used list continues to be not loaded once the Favourite button is lengthy-clicked on.

Actually, my purpose is to really make it easy to load the widely used list within the application and permit customers to find the word(s) to find information about again.

Thank you for the help.

Look this situation that is easy to read text file in android.

Example

file should be stored in.

res/raw/ myFavourite.txt .

Just like your condition. How can I read a text file from the SD card in Android?

Another simple tutorial.How you can read text file. http://android-er.blogspot.com/2010/07/display-text-file-in-resraw_01.html