简体   繁体   中英

java line breaks in file


I am trying to write a file line by line using apache FileUtils.writeLines()
When I try to open the file (notepad++, editplus) it is with no line breaks.
(I am sending null to the encoding) thanks.

FileUtils.writeLines(new File(INDICES_AND_WEIGHTS_FILENAME), indicesAndWeightsParams.indicesParams,";");

where indicesParams is a list
public List indicesParams;

Make sure which version of writeLines() you're calling. There is one version which allows you to specify the line endings. If you pass an empty string there, you won't get any:

public static void writeLines(File file, Collection<String> lines, String lineEnding) throws IOException {

The encoding is always between the file and the collection, the line separator is always the last parameter.

Are you using code similar to this?

List<String> ls = new ArrayList<String>();
ls.add("aaa");
ls.add("bbbbb");

FileUtils.writeLines(new File("newfile.txt"), "UTF-8", ls);   // same effect as with "null" as encoding

After this code, newFile.txt does have newlines.

Using od -a newfile.txt generates (on Windows):

0000000   a   a   a  cr  nl   b   b   b   b   b  cr  nl
0000014

which shows that newlines really do exist.

只要在需要中断的地方添加“ \\ r \\ n”即可。

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