简体   繁体   English

android在.txt文件的开头添加文本行

[英]android add text line at the beginning of .txt file

I have this code 我有这个代码

 File gpxfile = new File(root, "mesaje.txt");

            BufferedWriter bW;

            bW = new BufferedWriter(new FileWriter(gpxfile, true));
            bW.write(format + " " + message + "\n-----------------------\n");
            bW.newLine();
            bW.flush();
            bW.close();

It writes a .txt file adding text after the last line. 它会写一个.txt文件,在最后一行之后添加文本。 I would like to add text on the first line, so that it will appear as the lastest information. 我想在第一行添加文本,以便它将显示为最新信息。 How can i do that? 我怎样才能做到这一点? Thanks! 谢谢!

Read the file, store it into a string. 读取文件,并将其存储为字符串。 Then simply rewrite the file by adding your new text and then the string saved from previous file. 然后,只需添加新的文本,然后添加前一个文件中保存的字符串,即可重写文件。 Plenty of examples on here for reading text files 这里有许多读取文本文件的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM