Java 如何使用 System.getProperty("line.separator").toString()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3516957/
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-14 01:27:30  来源:igfitidea点击:

How do I use System.getProperty("line.separator").toString()?

javaeol

提问by ejsuncy

I have a Tab-delimited String (representing a table) that is passed to my method. When I print it to the command line, it appears like a table with rows:

我有一个传递给我的方法的制表符分隔的字符串(代表一个表)。当我将它打印到命令行时,它看起来像一个带有行的表:

http://i.stack.imgur.com/2fAyq.gif

http://i.stack.imgur.com/2fAyq.gif

The command window is correctly buffered. My thinking is that there is definitely a new line character before or after each row.

命令窗口已正确缓冲。我的想法是每行之前或之后肯定有一个换行符。

My problem is that I want to split up the incoming string into individual strings representing the rows of the table. So far I have:

我的问题是我想将传入的字符串拆分为代表表行的单个字符串。到目前为止,我有:

private static final String newLine = System.getProperty("line.separator").toString();
private static final String tab = "\t";
private static String[] rows;
...

rows = tabDelimitedTable.split(newLine);    //problem is here

System.out.println();
System.out.println("################### start debug ####################");

System.out.println((tabDelimitedTable.contains(newLine)) ? "True" : "False");

System.out.println("#################### end debug###################");
System.out.println();

output:

输出:

################### start debug ####################
False
#################### end debug###################

Obviously there is something in the string telling the OS to start a new line. Yet it apparently contains no newline characters.

显然,字符串中有一些东西告诉操作系统开始一个新行。然而它显然不包含换行符。

Running the latest JDK on Windows XP SP3.

在 Windows XP SP3 上运行最新的 JDK。

Any Ideas?

有任何想法吗?

采纳答案by Kerem Baydo?an

Try

尝试

rows = tabDelimitedTable.split("[" + newLine + "]");

This should solve the regexproblem.

这应该可以解决正则表达式问题。

Also not that important but return type of

也不是那么重要,但返回类型

System.getProperty("line.separator")

is Stringso no need to call toString().

字符串所以不需要调用 toString()。

回答by rmeador

I think your problem is that String.split()treats its argument as a regex, and regexes treat newlines specially. You may need to explicitly create a regex object to pass to split()(there is another overload of it) and configure that regex to allow newlines by passing MULTILINEin the flags param of Pattern.compile(). Docs

我认为您的问题是String.split()将其参数视为正则表达式,而正则表达式则专门对待换行符。您可能需要显式地创建一个正则表达式对象传递给split()(那里是它的另一个重载)并配置正则表达式来通过传递允许换行MULTILINE中的标志PARAM Pattern.compile()文档

回答by Brian Agnew

On Windows, line.separator is a CR/LF combination (reference here).

在 Windows 上, line.separator 是 CR/LF 组合(参考此处)。

The Java String.split()method takes a regular expression. So I think there's some confusion here.

JavaString.split()方法采用正则表达式。所以我认为这里有些混乱。

回答by Mike Baranczak

The other responders are correct that split() takes a regex as the argument, so you'll have to fix that first. The other problem is that you're assuming that the line break characters are the same as the system default. Depending on where the data is coming from, and where the program is running, this assumption may not be correct.

其他响应者认为 split() 将正则表达式作为参数是正确的,因此您必须先解决这个问题。另一个问题是您假设换行符与系统默认值相同。根据数据来自何处以及程序在何处运行,此假设可能不正确。

回答by James Van Huis

Try this:

尝试这个:

rows = tabDelimitedTable.split("[\r\n]+");

This should work regardless of what line delimiters are in the input, and will ignore blank lines.

无论输入中的行分隔符如何,这都应该有效,并且将忽略空行。

回答by polygenelubricants

The problem

问题

You must NOTassume that an arbitrary input text file uses the "correct" platform-specific newlineseparator. This seems to be the source of your problem; it has little to do with regex.

不能假设任意输入文本文件使用“正确的”特定于平台的换行符。这似乎是您问题的根源;它与正则表达式关系不大。

To illustrate, on the Windows platform, System.getProperty("line.separator")is "\r\n"(CR+LF). However, when you run your Java code on this platform, you may very well have to deal with an input file whose line separator is simply "\n"(LF). Maybe this file was originally created in Unix platform, and then transferred in binary (instead of text) mode to Windows. There could be many scenarios where you may run into these kinds of situations, where you must parse a text file as input which does not use the current platform's newline separator.

举例说明,在Windows平台上,System.getProperty("line.separator")"\r\n"(CR+LF)。但是,当您在此平台上运行 Java 代码时,您很可能不得不处理行分隔符只是"\n"(LF)的输入文件。也许这个文件最初是在 Unix 平台上创建的,然后以二进制(而不是文本)模式传输到 Windows。在许多情况下,您可能会遇到这些情况,您必须将文本文件解析为不使用当前平台的换行符的输入。

(Coincidentally, when a Windows text file is transferred to Unix in binary mode, many editors would display ^Mwhich confused some people who didn't understand what was going on).

(巧合的是,当 Windows 文本文件以二进制模式传输到 Unix 时,许多编辑器会显示^M,这让一些不了解发生了什么的人感到困惑)。

When you are producinga text file as output, you should probably prefer the platform-specific newline separator, but when you are consuminga text file as input, it's probably not safe to make the assumption that it correctly uses the platform specific newline separator.

当您生成文本文件作为输出时,您可能应该更喜欢特定于平台的换行符,但是当您使用文本文件作为输入时,假设它正确使用特定于平台的换行符可能并不安全。



The solution

解决方案

One way to solve the problem is to use e.g. java.util.Scanner. It has a nextLine()method that can return the next line (if one exists), correctly handling any inconsistency between the platform's newline separator and the input text file.

解决问题的一种方法是使用例如java.util.Scanner. 它有一个nextLine()方法可以返回下一行(如果存在),正确处理平台的换行符和输入文本文件之间的任何不一致。

You can also combine 2 Scanner, one to scan the file line by line, and another to scan the tokens of each line. Here's a simple usage example that breaks each line into a List<String>. The entire file therefore becomes a List<List<String>>.

您还可以组合 2 Scanner,一个是逐行扫描文件,另一个是扫描每行的标记。这是一个简单的用法示例,它将每一行分成一个List<String>. 因此整个文件变成了List<List<String>>.

This is probably a better approach than reading the entire file into one huge Stringand then splitinto lines (which are then splitinto parts).

这可能比将整个文件读成一个大文件String然后split读成行(然后split分成几部分)更好的方法。

    String text
        = "row1\tblah\tblah\tblah\n"
        + "row2\t1\t2\t3\t4\r\n"
        + "row3\tA\tB\tC\r"
        + "row4";

    System.out.println(text);
    //  row1    blah    blah    blah
    //  row2    1   2   3   4
    //  row3    A   B   C
    //  row4

    List<List<String>> input = new ArrayList<List<String>>();

    Scanner sc = new Scanner(text);
    while (sc.hasNextLine()) {
        Scanner lineSc = new Scanner(sc.nextLine()).useDelimiter("\t");
        List<String> line = new ArrayList<String>();
        while (lineSc.hasNext()) {
            line.add(lineSc.next());
        }
        input.add(line);
    }
    System.out.println(input);
    // [[row1, blah, blah, blah], [row2, 1, 2, 3, 4], [row3, A, B, C], [row4]]

See also

也可以看看

  • Effective Java 2nd Edition, Item 25: Prefer lists to arrays
  • Effective Java 第 2 版,第 25 条:列表优先于数组

Related questions

相关问题

回答by user207421

Try BufferedReader.readLine()instead of all this complication. It will recognize all possible line terminators.

尝试BufferedReader.readLine()而不是所有这些并发症。它将识别所有可能的行终止符。