Java FileWriter Output Wrap

  • 2021-11-02 00:46:57
  • OfStack

FileWriter Output Wrap

Use Java Adj. FileWriter Write a file, and when you need to break lines, assume that you have created a new one FileWriter fr ,

In the Windows operating system:

fr.write("\n"); It won't work,

Need fr.write("\r\n"); That is, enter and wrap

And in the Linux system:

As long as fr.write("\n"); You can

In order for the program to run on different operating systems, it can be written as


fr.write(System.getProperty("line.separator"));

Line breaks and appends for FileWriter

1. Wrap data

\n You can implement line wrapping, but windows Notepad, which comes with the system, opens without wrapping, because wiindows The recognized line break is not \n , but FileWriter0

For example: fw.write("\r\n");

[Note]: windows: FileWriter0

Linux: \n

Mac: \r

2. Additional writing of data

Construction method:


FileWriter(String fileName,boolean append)

For example:


FileWriter  fw = new FileWriter("a.txt",true);   // Represents an appended write, and the default is false

Related articles: