后得到一个“^ M”中的每一行从java创建unix文件

时间:2022-01-26 00:24:25

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

我有一个Java程序,它创建一个文件并用这个语句打印大量数据:

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?

当我查看这个文件在vim在Unix中每一行后,我看到一个^ M。它是什么?是什么原因导致了晒伤的?我怎么才能摆脱它呢?

5 个解决方案

#1


4  

^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.

^ M是字符13(十进制)这是回车(在代码\ r)。注意M是字母表中的第13个字母。

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.

您可以通过在代码中不包含\r来摆脱它。如果您使用的是unix平台,这将很好地工作。在windows上,这个文件看起来很有趣,除非你在记事本上看到它。

#2


8  

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");.

在构建输出字符串时,需要使用特定于平台的行分隔符字符串,而不是\r\n。可以通过System.getProperty(“line.separator”)获得。

#3


1  

*nix uses \n for newline, Windows uses \r\n and produces that ^M character in vi and the like.

* nix使用\ n换行,Windows使用\ r \ n和M ^字符产生在vi中等等。

#4


1  

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

你可能想要尝试运行文件通过在* nix dos2unix实用程序,它将摆脱^ M

#5


0  

You'll generally only see those if the first line was a unix line ending (lf) but it also includes DOS line endings. To remove them (and correct the file), load it again using :e ++ff=dos, then :set ff=unix, then write it.

通常,只有当第一行是unix行结束符(lf)时,您才会看到这些内容,但它也包括DOS行结束符。要删除它们(并修改文件),请再次使用:e ++ff=dos加载它,然后:set ff=unix,然后编写它。

Within the Java code, if you're writing text data instead of binary, use a PrintStream and use print() and println() which adds the correct line ending for your system.

在Java代码中,如果您正在编写文本数据而不是二进制数据,请使用PrintStream并使用print()和println(),这将为您的系统添加正确的行尾。

#1


4  

^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.

^ M是字符13(十进制)这是回车(在代码\ r)。注意M是字母表中的第13个字母。

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.

您可以通过在代码中不包含\r来摆脱它。如果您使用的是unix平台,这将很好地工作。在windows上,这个文件看起来很有趣,除非你在记事本上看到它。

#2


8  

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");.

在构建输出字符串时,需要使用特定于平台的行分隔符字符串,而不是\r\n。可以通过System.getProperty(“line.separator”)获得。

#3


1  

*nix uses \n for newline, Windows uses \r\n and produces that ^M character in vi and the like.

* nix使用\ n换行,Windows使用\ r \ n和M ^字符产生在vi中等等。

#4


1  

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

你可能想要尝试运行文件通过在* nix dos2unix实用程序,它将摆脱^ M

#5


0  

You'll generally only see those if the first line was a unix line ending (lf) but it also includes DOS line endings. To remove them (and correct the file), load it again using :e ++ff=dos, then :set ff=unix, then write it.

通常,只有当第一行是unix行结束符(lf)时,您才会看到这些内容,但它也包括DOS行结束符。要删除它们(并修改文件),请再次使用:e ++ff=dos加载它,然后:set ff=unix,然后编写它。

Within the Java code, if you're writing text data instead of binary, use a PrintStream and use print() and println() which adds the correct line ending for your system.

在Java代码中,如果您正在编写文本数据而不是二进制数据,请使用PrintStream并使用print()和println(),这将为您的系统添加正确的行尾。