Writing String to Text File

后端 未结 6 1677
太阳男子
太阳男子 2021-01-29 01:21

I am saving a log to a .txt file on the sdcard but once there is two lines saved, it overwrites it and starts over?

Here is my code:

public static Strin         


        
6条回答
  •  一生所求
    2021-01-29 01:42

    Using NIO you can write to the end of the file in more efficient way

    public static void writeToLog(String string) {
       String text = getTimestamp() + " " + string;
    
       try {
          java.nio.file.Files.write(java.nio.file.Paths.get("/file.txt"), text.getBytes(), java.nio.file.StandardOpenOptions.APPEND);
       } catch (IOException ignore) { }
    }
    

提交回复
热议问题