Java filewriter output newline operation

Time:2022-7-31

Filewriter output wrap

useJavaofFileWriterWhen writing a file and a line break is required, it is assumed that it has been createdFileWriter fr,

In Windows operating system:

fr.write("\n");It’s impossible,

needfr.write("\r\n");That is, enter and change lines

In Linux system:

as long asfr.write("\n");that will do

In order that the program can run in different operating systems, it can be written as


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

Line feed and append of filewriter

1. Line feed of data

\nLine breaks can be achieved, butwindowsThe Notepad provided by the system opens without line feed becausewiindowsRecognized newline is not\n, but\r\n

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

[note]: Windows:\r\n

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);   // Indicates additional write. The default is false

The above is my personal experience. I hope I can give you a reference, and I hope you can support developeppaer.

Recommended Today

JS generate guid method

JS generate guid method https://blog.csdn.net/Alive_tree/article/details/87942348 Globally unique identification(GUID) is an algorithm generatedBinaryCount Reg128 bitsNumber ofidentifier , GUID is mainly used in networks or systems with multiple nodes and computers. Ideally, any computational geometry computer cluster will not generate two identical guids, and the total number of guids is2^128In theory, it is difficult to make two […]