Java 如何在Android中写入和读取文本文件?

Java 如何在Android中写入和读取文本文件?,java,android,database,text,savestate,Java,Android,Database,Text,Savestate,我正在开发一个字典应用程序。应用程序上有一个收藏夹按钮,允许用户: 短按可将当前查看的单词添加到收藏夹列表中 长时间单击可查看喜爱的列表(添加的单词) 到目前为止,我的代码如下: 更新代码: //Writing lines to myFavourite.txt btnAddFavourite = (ImageButton) findViewById(R.id.btnAddFavourite); btnAddFavourite.setOnClickListener(new Vi

我正在开发一个字典应用程序。应用程序上有一个收藏夹按钮,允许用户:

  • 短按可将当前查看的单词添加到收藏夹列表中
  • 长时间单击可查看喜爱的列表(添加的单词)
到目前为止,我的代码如下:

更新代码:

//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;

        }});
        }
但是,“收藏夹”按钮不适用于上述代码行

文件myfavorite.txt确实存在(在Eclipse的data/data/my_project/files中),但它只包含一个最近添加的单词。此外,当长时间单击收藏夹按钮时,应用程序将强制关闭

我做错了什么?如果你们能帮我解决这个问题,我非常感激。多谢各位

========

编辑


非常感谢你的帮助。我更新了代码以反映您的评论和提示。到目前为止,已经有了一些改进:最喜欢的单词已经被写入到文件myfavorite.txt中,就像word2 word2 word3。。。(尽管我希望它们出现在新的行中)

但是,当长时间单击“收藏夹”按钮时,仍不会加载收藏夹列表

事实上,我的目的是在应用程序中加载收藏夹列表,并允许用户选择要再次查找的单词

非常感谢您的帮助。

在这条线上

OutputStreamWriter out = new OutputStreamWriter(openFileOutput("myFavourite.txt",0));
如果每次创建流时该文件已经存在,则将覆盖该文件。您要做的是传递模式_APPEND,而不是0。看一看

关于长时间的点击,这些线

if (instream) {

甚至不应该编译。你想要的可能是

if (instream.ready()) {

在这条线上

OutputStreamWriter out = new OutputStreamWriter(openFileOutput("myFavourite.txt",0));
如果每次创建流时该文件已经存在,则将覆盖该文件。您要做的是传递模式_APPEND,而不是0。看一看

关于长时间的点击,这些线

if (instream) {

甚至不应该编译。你想要的可能是

if (instream.ready()) {


看看这个例子,它在android中很容易读取文本文件

文件必须保存在文件中

res/raw/myfavorite.txt

和你的问题一样

另一个简单的教程。如何阅读文本文件。

看看这个例子,它在android中很容易读取文本文件

文件必须保存在文件中

res/raw/myfavorite.txt

和你的问题一样

另一个简单的教程。如何阅读文本文件。

非常感谢您的帮助。请看一下我的更新代码,它在一定程度上反映了您的意见。非常感谢您的帮助。请看一下我的更新代码,它在一定程度上反映了您的意见。非常感谢您的帮助。请看一下我的更新代码,它在一定程度上反映了您的意见。非常感谢您的帮助。请看一下我的更新代码,它在一定程度上反映了您的意见。