android - 如何在android中附加数据(每个数据换行)

标签 android file-io fileoutputstream

我正在为网络浏览器应用程序制作一个书签,这段代码正在保存和加载数据,但它没有在新行中附加数据......每次我按下按钮它都会覆盖以前的数据......我希望每次当我在主要 Activity 中调用 bookmarkload(); 方法时,它应该将数据保存在新行中而不是覆盖它..请帮助我,因为我是 android 的新手,告诉我在哪一行输入..这样它开始附加数据..提前致谢..请尽可能详细地回答。

public class Bookmark {

FileOutputStream fos;
FileInputStream fis = null;

public void bookmarksave(Context context,String FILENAME,String data){

    try {
        fos = context.openFileOutput(FILENAME, 0);
        fos.write(data.getBytes());
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public String bookmarkload(Context context,String FILENAME){

    String collected =null;
    try{
        fis =context.openFileInput(FILENAME);
        byte[] dataArray = new byte[fis.available()];
        while(fis.read(dataArray) != -1){
            collected = new String(dataArray);
            fis.close();
        }
    } catch(FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return collected;
}
}

最佳答案

fos.write(System.getProperty("line.separator").getBytes());

试试这个...新数据将追加到新行中..

关于android - 如何在android中附加数据(每个数据换行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21327452/

相关文章:

找不到名称为 'kapt' 的 Android 配置

android - 如何在 Android 4 中获取 webview 比例

Android Studio - 无法在设备上运行应用程序 minSdk(API 23, N) != device Sdk(API 22)

arrays - 弹出数组的键以计算总数

c - 如何保存文件内容

c - 错误 : expected '=' , ',' , ';' , 'asm' 或 '__attribute__' 在 C 中的 '*' token 之前

android - android 中的 IllegalArgumentException : x + width must be <= bitmap. width()

java - 从服务器发送字符串到客户端 - 将其保存为文本文件

java - 文件传输服务器 <-> 客户端,文件不完整

java - 写入 .txt 文件时出现奇怪的 "box"符号