Getting "^ M" after every line in unix from java file created

I have a Java program that creates a file and prints a bunch of data using this statement:

out.write(data+"|"+data2+"\r\n"); 

When I view this file in vim on Unix, I see a ^ M after each line. What is it? What causes this? How can I get rid of it?

+4
source share
5 answers

^ M is the character 13 (decimal), which is a carriage return (in your code \r ). Note that M is the 13th letter of the alphabet.

You can get rid of it by not including \r in your code. This will work fine if you are on a unix platform. In windows, the file will look funny if you do not view it in something like Wordpad.

+4
source

When building the output string, you need to use the string string of the line separator instead of \r\n . This can be obtained using System.getProperty("line.separator"); .

+8
source

* nix uses \ n for a new line, Windows uses \ r \ n and produces the ^ M character in vi, etc.

+1
source

You might want to try running the file using the dos2unix utility in * nix, it will get rid of ^M

+1
source

Usually you will only see those if the first line was the end of the unix (lf) line, but it also includes the end of the DOS lines. To delete them (and fix the file), download it again using: e ++ ff = dos, then: set ff = unix, then write.

In Java code, if you write text data instead of binary, use PrintStream and use print () and println (), which adds the correct line ending for your system.

0
source

Source: https://habr.com/ru/post/1379688/


All Articles