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

标签 java android database text savestate

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

  • 短按将当前查看的单词添加到 Collection 列表;
  • 长按可查看(已添加单词的) Collection 夹列表。

到目前为止,我已经编码如下:

更新代码:

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

        }});
        }

但是, Collection 夹按钮不适用于上述代码行。

myFavourite.txt 文件确实存在(在 Eclipse 中的 data/data/my_project/files 中)但它只包含一个最近添加的单词。此外,长按“Collection 夹”按钮时,应用程序会被强制关闭。

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

========

编辑

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

但是,当 Collection 夹按钮被长按时, Collection 夹列表仍然没有加载。

事实上,我的目的是让在应用程序中加载 Collection 夹列表并允许用户选择要再次查找的单词成为可能。

非常感谢您的帮助。

最佳答案

在这条线上

OutputStreamWriter out = new OutputStreamWriter(openFileOutput("myFavourite.txt",0));

如果您每次创建流时文件已经存在,那么您将覆盖该文件。你想要做的是传递 MODE_APPEND 而不是 0。看看 the documentation .

关于长时间点击,这些行

if (instream) {

while (( line = buffreader.readLine())) {

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

if (instream.ready()) {

while ((line = buffreader.readLine()) != null) {
    // Use this line
}

关于java - 如何在 Android 中写入和读取文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8026204/

相关文章:

android - 改造显示进度对话框

android - 如何更改图表标题的颜色

java - Oracle 只读 JDBC 连接

mysql - 如何管理大型视频数据库?

android - 更改按钮的背景图像

sql - 用于具有多个角色/权限的用户管理的用户表设计

java - Spring使用ref标签的本地属性引用存在于其他xml中的bean

java - 当鼠标移动到字母上时显示字母

javascript - 如何在 Bluemix 上的 Websphere liberty 配置文件中启用 javax.script?

java - 访问未定义值后 Nashorn 中的输出不一致