简体   繁体   中英

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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