简体   繁体   English

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

[英]How to remove line breaks from a file in Java?

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.)?如何在 Java 中以适用于 Windows 和 Linux 的方式替换字符串中的所有换行符(即没有回车/换行/换行等操作系统特定问题)?

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?如何才能做到这一点?

You need to set text to 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 replace doesn't change the original String, it returns a new one that's been changed.这是必要的,因为字符串是不可变的——调用replace不会更改原始字符串,它返回一个已更改的新字符串。 If you don't assign the result to text , then that new String is lost and garbage collected.如果您不将结果分配给text ,则该新 String 将丢失并被垃圾收集。

As for getting the newline String for any environment -- that is available by calling System.getProperty("line.separator") .至于为任何环境获取换行符字符串——可通过调用System.getProperty("line.separator")

As noted in other answers, your code is not working primarily because String.replace(...) does not change the target String .正如其他答案中所述,您的代码不起作用主要是因为String.replace(...)不会更改目标String (It can't - Java strings are immutable!) What replace actually does is to create and return a new String object with the characters changed as required. (它不能 - Java 字符串是不可变的!) replace实际上所做的是创建并返回一个新的String对象,其中的字符根据需要进行了更改。 But your code then throws away that 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.删除所有 Windows、UNIX 或 Mac 行终止符。 However, if the input file is text, this will concatenate words;但是,如果输入文件是文本,这将连接单词; eg例如

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 1 .它用空格1替换每个行终止符。 Since Java 8 you can also do this:从 Java 8 开始,您也可以这样做:

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

And if you want to replace multiple line terminator with one space:如果你想用一个空格替换多行终止符:

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

1 - Note there is a subtle difference between #3 and #4. 1 - 请注意,#3 和 #4 之间存在细微差别。 The sequence \\r\\n represents a single (Windows) line terminator, so we need to be careful not to replace it with two spaces.序列\\r\\n表示单个(Windows)行终止符,因此我们需要注意不要将其替换为两个空格。

This function normalizes down all whitespace, including line breaks, to single spaces. 此函数将所有空格(包括换行符)规范化为单个空格。 Not exactly what the original question asked for, but likely to do exactly what is needed in many cases:不完全是原始问题所要求的,但可能会在许多情况下完全满足需要:

import org.apache.commons.lang3.StringUtils;

final String cleansedString = StringUtils.normalizeSpace(rawString);

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

This would be efficient I guess我猜这会很有效

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

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

Linebreaks are not the same under windows/linux/mac. windows/linux/mac 下的换行符是不一样的。 You should use System.getProperties with the attribute line.separator.您应该将System.getProperties与属性 line.separator 一起使用。

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();简而言之String text = readFileAsString("textfile.txt").trim(); will also work for you.也会为你工作。 (Checked with Java 6) (用 Java 6 检查)

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

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

You may want to read your file with a BufferedReader .您可能希望使用BufferedReader读取您的文件。 This class can break input into individual lines, which you can assemble at will.这个类可以将输入分成单独的行,您可以随意组合。 The way BufferedReader operates recognizes line ending conventions of the Linux, Windows and MacOS worlds automatically, regardless of the current platform.无论当前平台如何, 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.请注意, readLine()在返回的字符串中不包含行终止符。 The code above appends a space to avoid gluing together the last word of a line and the first word of the next line.上面的代码添加了一个空格,以避免将一行的最后一个单词和下一行的第一个单词粘在一起。

I find it odd that (Apache) StringUtils wasn't covered here yet.我觉得奇怪的是(Apache) StringUtils还没有在这里介绍。

you can remove all newlines (or any other occurences of a substring for that matter) from a string using the .replace method您可以使用.replace方法从字符串中删除所有换行符(或任何其他出现的子字符串)

StringUtils.replace(myString, "\n", "");

This line will replace all newlines with the empty string.此行将用空字符串替换所有换行符。

because newline is technically a character you can optionally use the .replaceChars method that will replace characters因为换行符在技术上是一个字符,您可以选择使用.replaceChars方法来替换字符

StringUtils.replaceChars(myString, '\n', '');

You can use apache commons IOUtils to iterate through the line and append each line to StringBuilder.您可以使用 apache commons IOUtils 遍历该行并将每一行附加到 StringBuilder。 And don't forget to close the InputStream并且不要忘记关闭 InputStream

StringBuilder sb = new StringBuilder();
FileInputStream fin=new FileInputStream("textfile.txt");
LineIterator lt=IOUtils.lineIterator(fin, "utf-8");
while(lt.hasNext())
{
  sb.append(lt.nextLine());
}
String text = sb.toString();
IOUtils.closeQuitely(fin);

FYI if you can want to replace simultaneous muti-linebreaks with single line break then you can use仅供参考,如果您想用单换行符替换同时的多换行符,那么您可以使用

myString.trim().replaceAll("[\n]{2,}", "\n")

Or replace with a single space或者用一个空格替换

myString.trim().replaceAll("[\n]{2,}", " ")

You can use generic methods to replace any char with any char.您可以使用通用方法将任何字符替换为任何字符。

public static void removeWithAnyChar(String str, char replceChar,
        char replaceWith) {
    char chrs[] = str.toCharArray();
    int i = 0;
    while (i < chrs.length) {

        if (chrs[i] == replceChar) {
            chrs[i] = replaceWith;
        }
        i++;
    }

}

In Kotlin, and also since Java 11, String has lines() method, which returns list of lines in the multi-line string.在 Kotlin 和 Java 11 中, Stringlines()方法,它返回多行字符串中的行列表。 You can get all the lines and then merge them into a single string.您可以获取所有行,然后将它们合并为一个字符串。

With Kotlin it will be as simple as使用 Kotlin 将变得如此简单

str.lines().joinToString("")

org.apache.commons.lang.StringUtils#chopNewline

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM