Android的写文件 [英] Android write file

查看:114
本文介绍了Android的写文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

周不是编程之后,我决定结束我的申请。上次我没能做到文件的写入和读出,现在我想这样做。我也许可以使用数据库但这似乎轻松了许多。 我发现从这个页面在那里我复制粘贴的code。至少现在我可以检查文件的内容,我的手机上(我没有使用任何虚拟SD卡暂时的,但它肯定会加速测试)。 问题是这个code,每次我写的文件时,请关闭我的应用程序,然后重新打开它,并再次写入文件,该文件的内容将被重置。

 文件根= Environment.getExternalStorageDirectory();
档案文件=新的文件(根,tomato50.txt);
如果(assignArr.size()大于0){
    尝试 {
        如果(root.canWrite()){
            的FileWriter FileWriter的=新的FileWriter(文件);
            BufferedWriter将出=新的BufferedWriter(的FileWriter);
            的for(int i = 0; I< assignArr.size();我++){
                out.write(assignArr.get(ⅰ)+\ñ);
                Toast.makeText(MainActivity.this,
                        指出:+ assignArr.get(I),Toast.LENGTH_LONG)
                        。显示();
            }
            out.close();
        }
    }赶上(IOException异常E){
        Log.e(TAG,无法写入文件+ e.getMessage());
    }
}
 

例如。

  1. 我把一个项目到assignArr。
  2. 的项被写入到文件中。
  3. 我关闭应用程序。 AssignArr得到空,因为它是一个变量。
  4. 我打开应用程序。
  5. 在我把物品放入assignArr。
  6. 我关闭应用程序。 AssignArr得到空,因为它是一个变量。
  7. 在我打开一个显示只有最后一个项目文件。
解决方案

使用<一个href="http://download.oracle.com/javase/6/docs/api/java/io/FileWriter.html#FileWriter%28java.io.File,%20boolean%29"><$c$c>FileWriter FileWriter的=新的FileWriter(文件,真); 将数据追加到现有文件

After weeks of not programming I've decided to finish my application. Last time I was not able to do the file writing and reading and now I want to do it. I could perhaps use databases but this seems a lot easier. I have found this page from where I copy-pasted the code. At least now I am able to check the content of the file on my phone (I am not using any virtual SD cards for the time being, but it would definitely accelerate the testing). Problem is with this code that every time I write the file, close my app and then reopen it and write the file again, the content of the file is reset.

File root = Environment.getExternalStorageDirectory();
File file = new File(root, "tomato50.txt");
if (assignArr.size() > 0) {
    try {
        if (root.canWrite()) {
            FileWriter filewriter = new FileWriter(file);
            BufferedWriter out = new BufferedWriter(filewriter);
            for (int i = 0; i < assignArr.size(); i++) {
                out.write(assignArr.get(i) + "\n");
                Toast.makeText(MainActivity.this,
                        "out: " + assignArr.get(i), Toast.LENGTH_LONG)
                        .show();
            }
            out.close();
        }
    } catch (IOException e) {
        Log.e("TAG", "Could not write file " + e.getMessage());
    }
}

E.g.

  1. I put one item into assignArr.
  2. The item is written into the file.
  3. I close the app. AssignArr gets empty as it is a variable.
  4. I open the app.
  5. I put an item into assignArr.
  6. I close the app. AssignArr gets empty as it is a variable.
  7. I open the file which shows only the last item.

解决方案

Use FileWriter filewriter = new FileWriter(file,true); to append data to the existing file.

这篇关于Android的写文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆