Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting a "^M" after each line in unix from a java created file

Tags:

java

vim

unix

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 in Unix I see a ^M after each line. What is it? What is causing this? How can I get rid of it?

like image 544
Mike Avatar asked Dec 10 '22 04:12

Mike


2 Answers

You need to use the platform-specific line separator string instead of \r\n when constructing your output string. That can be obtained by System.getProperty("line.separator");.

like image 121
Ted Hopp Avatar answered Jan 26 '23 01:01

Ted Hopp


^M is character 13 (decimal) which is the carriage return (in your code it's \r). Notice 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're on a unix platform. On windows, the file will look funny unless you're viewing it in something like Wordpad.

like image 29
Jonathan M Avatar answered Jan 26 '23 01:01

Jonathan M