How to append data to a file?

后端 未结 4 938
长情又很酷
长情又很酷 2020-11-30 14:43

I trying to write into the txt file.
But with out losing the data that is already stored in the file.

But my problem that when I put string in the txt file, the

相关标签:
4条回答
  • 2020-11-30 14:55

    You have to set the Stream to append mode like this:

    pw = new PrintWriter(new FileWriter("d:\\stam\\stam.txt", true));
    
    0 讨论(0)
  • 2020-11-30 14:57

    Use a FileWriter with second argument 'true' and a BufferedWriter:

    FileWriter writer = new FileWriter("outFile.txt", true);
    BufferedWriter out = new BufferedWriter(writer);
    
    0 讨论(0)
  • 2020-11-30 15:03

    In Java 7,

    Files.write(FileSystems.getDefault().getPath(targetDir,fileName),
    strContent.getBytes(),StandardOpenOption.CREATE,StandardOpenOption.APPEND);
    
    0 讨论(0)
  • 2020-11-30 15:15

    Create a separate variable to emphasize that, appending to file.

    boolean append = true;
    pw = new PrintWriter(new FileWriter(new File("filepath.txt"), append));
    
    0 讨论(0)
提交回复
热议问题