如何找出哪个行分隔符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)之后,似乎没有一种确定特定文件中使用的行尾编码的干净方法。 我推荐的最好的办法是使用ѭ1并遍历文件中的每个字符。像这样:
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 = \"\";
}
    

bab

为了与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以包括所需的其他功能。     
BufferedReader.readLine()
不提供确定换行符的任何方法。如果您需要知道,则需要阅读自己的字符并找到换行符。 您可能对Guava的内部LineBuffer类(以及用于其中的公共LineReader类)感兴趣。
LineBuffer
提供回调方法
void handleLine(String line, String end)
,其中
end
是换行符。您可能可以基于此做您想做的事。 API可能类似于“ 10”,其中“ 11”是一个包含行文本和行尾的对象。     
答案是您找不到行的结尾。 我正在寻找可以在同一函数中导致行尾的内容。在查看BufferedReader源代码之后,我可以说BufferedReader.readLine在\'\\ r \'或\'\\ n \'上结束行,并跳过左下方的\'\\ r \'或\'\\ n \ '。硬编码,不关心设置。     
如果碰巧将此文件读取到Swing文本组件中,则可以使用JTextComponent.read(...)方法将文件加载到Document中。然后,您可以使用:
textComponent.getDocument().getProperty( DefaultEditorKit.EndOfLineStringProperty );
获取文件中使用的实际EOL字符串。     
不确定是否有用,但有时我需要在已经很远的位置读取文件后找出行定界符。 在这种情况下,我使用以下代码:
/**
* <h1> Identify which line delimiter is used in a string </h1>
*
* 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}, <br>
*           unix/linux {@code \\n} or legacy mac {@code \\r} <br>
*           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\'
    

要回复问题请先登录注册