如何找出用于分割线的BufferedReader#readLine()哪个行分隔符?

我正在通过BufferedReader读取文件

String filename = ... br = new BufferedReader( new FileInputStream(filename)); while (true) { String s = br.readLine(); if (s == null) break; ... } 

我需要知道线条是否以’\ n’或’\ r \ n’分隔是否有我能找到的方法?

我不想打开FileInputStream以便最初扫描它。 理想情况下,我想询问BufferedReader,因为它必须知道。

我很高兴覆盖BufferedReader来破解它,但我真的不想打开文件流两次。

谢谢,

注意:当前行分隔符(由System.getProperty(“line.separator”)返回)无法使用,因为该文件可能已由另一个应用程序在另一个操作系统上写入。

在阅读了java文档 (我承认自己是一个pythonista)之后,似乎没有一种干净的方法来确定特定文件中使用的行端编码。

我建议的最好的事情是你使用BufferedReader.read()并迭代文件中的每个字符。 像这样的东西:

 String filename = ... br = new BufferedReader( new FileInputStream(filename)); while (true) { String l = ""; Char c = " "; while (true){ c = br.read(); if not c == "\n"{ // do stuff, not sure what you want with the endl encoding // break to return endl-free line } if not c == "\r"{ // do stuff, not sure what you want with the endl encoding // break to return endl-free line Char ctwo = ' ' ctwo = br.read(); if ctwo == "\n"{ // do extra stuff since you know that you've got a \r\n } } else{ l = l + c; } if (l == null) break; ... l = ""; } 

要与BufferedReader类同步,您可以使用以下方法处理\ n,\ r,\ n \ r和\ r \ n结束行分隔符:

 public static String retrieveLineSeparator(File file) throws IOException { char current; String lineSeparator = ""; FileInputStream fis = new FileInputStream(file); try { while (fis.available() > 0) { current = (char) fis.read(); if ((current == '\n') || (current == '\r')) { lineSeparator += current; if (fis.available() > 0) { char next = (char) fis.read(); if ((next != current) && ((next == '\r') || (next == '\n'))) { lineSeparator += next; } } return lineSeparator; } } } finally { if (fis!=null) { fis.close(); } } return null; } 

BufferedReader不接受FileInputStreams

不,您无法找到BufferedReader正在读取的文件中使用的行终止符。 读取文件时丢失了该信息。

不幸的是,以下所有答案都是错误的。

编辑:是的,您可以随时扩展BufferedReader以包含您想要的其他function。

BufferedReader.readLine()不提供任何确定换行符的方法。 如果你需要知道,你需要自己阅读字符并自己找到换行符。

您可能对Guava的内部LineBuffer类(以及它所使用的公共LineReader类) 感兴趣 。 LineBuffer提供了一个回调方法void handleLine(String line, String end) ,其中end是换行符。 你可以根据自己的需要做一些事情。 API可能看起来像public Line readLine() ,其中Line是包含行文本和行结尾的对象。

答案是你无法找出线的结尾是什么。

我正在寻找可以在同一个函数中导​​致行结尾的内容。 在查看BufferedReader源代码之后,我可以认为BufferedReader.readLine在’\ r’或’\ n’上结束行并跳过leftower’\ r’或’\ n’。 硬编码,不关心设置。

如果您正在将此文件读入Swing文本组件,则可以使用JTextComponent.read(…)方法将文件加载到Document中。 然后你可以使用:

 textComponent.getDocument().getProperty( DefaultEditorKit.EndOfLineStringProperty ); 

获取文件中使用的实际EOL字符串。

不确定是否有用,但有时我需要在我读完文件后找到行分隔符。

在这种情况下,我使用此代码:

 /** * 

Identify which line delimiter is used in a string

* * This is useful when processing files that were created on different operating systems. * * @param str - the string with the mystery line delimiter. * @return the line delimiter for windows, {@code \r\n},
* unix/linux {@code \n} or legacy mac {@code \r}
* if none can be identified, it falls back to unix {@code \n} */ public static String identifyLineDelimiter(String str) { if (str.matches("(?s).*(\\r\\n).*")) { //Windows //$NON-NLS-1$ return "\r\n"; //$NON-NLS-1$ } else if (str.matches("(?s).*(\\n).*")) { //Unix/Linux //$NON-NLS-1$ return "\n"; //$NON-NLS-1$ } else if (str.matches("(?s).*(\\r).*")) { //Legacy mac os 9. Newer OS X use \n //$NON-NLS-1$ return "\r"; //$NON-NLS-1$ } else { return "\n"; //fallback onto '\n' if nothing matches. //$NON-NLS-1$ } }

如果你使用groovy,你可以简单地做:

 def lineSeparator = new File('path/to/file').text.contains('\r\n') ? '\r\n' : '\n'