如何从Java中的文件中删除换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2163045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:16:05  来源:igfitidea点击:

How to remove line breaks from a file in Java?

javastringnewlineline-breaks

提问by tree-hacker

How can I replace all line breaks from a string in Java in such a way that will work on Windows and Linux (ie no OS specific problems of carriage return/line feed/new line etc.)?

如何以适用于 Windows 和 Linux 的方式替换 Java 中字符串中的所有换行符(即没有回车/换行/换行等操作系统特定问题)?

I've tried (note readFileAsString is a function that reads a text file into a String):

我试过(注意 readFileAsString 是一个将文本文件读入字符串的函数):

String text = readFileAsString("textfile.txt");
text.replace("\n", "");

but this doesn't seem to work.

但这似乎不起作用。

How can this be done?

如何才能做到这一点?

采纳答案by Kaleb Brasee

You need to set textto the results of text.replace():

您需要设置text为以下结果text.replace()

String text = readFileAsString("textfile.txt");
text = text.replace("\n", "").replace("\r", "");

This is necessary because Strings are immutable -- calling replacedoesn't change the original String, it returns a new one that's been changed. If you don't assign the result to text, then that new String is lost and garbage collected.

这是必要的,因为字符串是不可变的——调用replace不会更改原始字符串,而是返回一个已更改的新字符串。如果您不将结果分配给text,则该新字符串将丢失并被垃圾收集。

As for getting the newline String for any environment -- that is available by calling System.getProperty("line.separator").

至于为任何环境获取换行符字符串——可以通过调用System.getProperty("line.separator").

回答by Viktor Klang

String text = readFileAsString("textfile.txt").replace("\n","");

.replace returns a new string, strings in Java are Immutable.

.replace 返回一个新字符串,Java 中的字符串是不可变的。

回答by Aif

Linebreaks are not the same under windows/linux/mac. You should use System.getPropertieswith the attribute line.separator.

windows/linux/mac 下的换行符是不一样的。您应该将System.getProperties与属性 line.separator 一起使用。

回答by Fabian Steeg

If you want to remove only line terminators that are valid on the current OS, you could do this:

如果您只想删除在当前操作系统上有效的行终止符,您可以这样做:

text = text.replaceAll(System.getProperty("line.separator"), "");

If you want to make sure you remove any line separators, you can do it like this:

如果要确保删除任何行分隔符,可以这样做:

text = text.replaceAll("\r|\n", "");

Or, slightly more verbose, but less regexy:

或者,稍微冗长一些,但不那么正则:

text = text.replaceAll("\r", "").replaceAll("\n", "");

回答by Stephen C

As noted in other answers, your code is not working primarilybecause String.replace(...)does not change the target String. (It can't - Java strings are immutable!) What replaceactually does is to create and return a new Stringobject with the characters changed as required. But your code then throws away that String...

正如其他答案中所述,您的代码无法正常工作主要是因为String.replace(...)没有更改 target String。(它不能 - Java 字符串是不可变的!)replace实际上是创建并返回一个新String对象,其中的字符根据需要进行了更改。但是你的代码然后扔掉了String......



Here are some possible solutions. Which one is most correct depends on what exactly you are trying to do.

以下是一些可能的解决方案。哪一个最正确取决于您究竟要做什么。

// #1
text = text.replace("\n", "");

Simply removes all the newline characters. This does not cope with Windows or Mac line terminations.

只需删除所有换行符。这不适用于 Windows 或 Mac 线路终止。

// #2
text = text.replace(System.getProperty("line.separator"), "");

Removes all line terminators for the current platform. This does not cope with the case where you are trying to process (for example) a UNIX file on Windows, or vice versa.

删除当前平台的所有行终止符。这不适用于您尝试在 Windows 上处理(例如)UNIX 文件的情况,反之亦然。

// #3
text = text.replaceAll("\r|\n", "");

Removes all Windows, UNIX or Mac line terminators. However, if the input file is text, this will concatenate words; e.g.

删除所有 Windows、UNIX 或 Mac 行终止符。但是,如果输入文件是文本,这将连接单词;例如

Goodbye cruel
world.

becomes

变成

Goodbye cruelworld.

So you might actually want to do this:

所以你可能真的想要这样做:

// #4
text = text.replaceAll("\r\n|\r|\n", " ");

which replaces each line terminator with a space. Since Java 8 you can also do this:

用空格替换每个行终止符。从 Java 8 开始,您也可以这样做:

// #5
text = text.replaceAll("\R", " ");

And if you want to replace multiple line separators with one space:

如果您想用一个空格替换多个行分隔符:

// #6
text = text.replaceAll("\R+", " ");

回答by Thomas Pornin

You may want to read your file with a BufferedReader. This class can break input into individual lines, which you can assemble at will. The way BufferedReaderoperates recognizes line ending conventions of the Linux, Windows and MacOS worlds automatically, regardless of the current platform.

您可能希望使用BufferedReader. 这个类可以将输入分成单独的行,您可以随意组合。该方法BufferedReader操作识别行结束了Linux的Windows的约定和MacOS世界自动,无论当前的平台。

Hence:

因此:

BufferedReader br = new BufferedReader(
    new InputStreamReader("textfile.txt"));
StringBuilder sb = new StringBuilder();
for (;;) {
    String line = br.readLine();
    if (line == null)
        break;
    sb.append(line);
    sb.append(' ');   // SEE BELOW
}
String text = sb.toString();

Note that readLine()does not include the line terminator in the returned string. The code above appends a space to avoid gluing together the last word of a line and the first word of the next line.

请注意,readLine()返回的字符串中不包括行终止符。上面的代码添加了一个空格,以避免将一行的最后一个单词和下一行的第一个单词粘在一起。

回答by MukeshKoshyM

String text = readFileAsString("textfile.txt").replaceAll("\n", "");

Even though the definition of trim() in oracle website is "Returns a copy of the string, with leading and trailing whitespace omitted."

即使oracle网站中trim()的定义是“返回字符串的副本,省略前导和尾随空格”。

the documentation omits to say that new line characters (leading and trailing) will also be removed.

文档没有说新行字符(前导和尾随)也将被删除。

In short String text = readFileAsString("textfile.txt").trim();will also work for you. (Checked with Java 6)

总之 String text = readFileAsString("textfile.txt").trim();也会为你工作。(用 Java 6 检查)

回答by seyf

Try doing this:

尝试这样做:

 textValue= textValue.replaceAll("\n", "");
 textValue= textValue.replaceAll("\t", "");
 textValue= textValue.replaceAll("\n", "");
 textValue= textValue.replaceAll("\t", "");
 textValue= textValue.replaceAll("\r", "");
 textValue= textValue.replaceAll("\r", "");
 textValue= textValue.replaceAll("\r\n", "");
 textValue= textValue.replaceAll("\r\n", "");

回答by JSBach

This would be efficient I guess

我猜这会很有效

String s;
s = "try this\n try me.";
s.replaceAll("[\r\n]+", "")

回答by Renán D

str = str.replaceAll("\r\n|\r|\n", " ");

Worked perfectly for me after searching a lot, having failed with every other line.

在搜索了很多之后,对我来说效果很好,其他每一行都失败了。