How to find out which line separator BufferedReader#readLine() used to split the line?

后端 未结 9 1398
星月不相逢
星月不相逢 2020-12-11 01:47

I am reading a file via the BufferedReader

String filename = ...
br = new BufferedReader( new FileInputStream(filename));
while (true) {
   String s = br.re         


        
相关标签:
9条回答
  • 2020-12-11 02:29

    The answer would be You can't find out what was the line ending.

    I am looking for what can cause line endings in the same funcion. After looking at the BufferedReader source code, I can saz that BufferedReader.readLine ends line on '\r' or '\n' and skips leftower '\r' or '\n'. Hardcoded, does not care about settings.

    0 讨论(0)
  • 2020-12-11 02:30

    To be in phase with the BufferedReader class, you may use the following method that handles \n, \r, \n\r and \r\n end line separators:

    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;
    }
    
    0 讨论(0)
  • 2020-12-11 02:33

    Not sure if useful, but sometimes I need to find out the line delimiter after I've read the file already far-down the road.

    In this case I use this code:

    /**
    * <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$
        }
    }
    
    0 讨论(0)
  • 2020-12-11 02:35

    BufferedReader.readLine() does not provide any means of determining what the line break was. If you need to know, you'll need to read characters in yourself and find line breaks yourself.

    You may be interested in the internal LineBuffer class from Guava (as well as the public LineReader class it's used in). LineBuffer provides a callback method void handleLine(String line, String end) where end is the line break characters. You could probably base something to do what you want on that. An API might look something like public Line readLine() where Line is an object that contains both the line text and the line end.

    0 讨论(0)
  • 2020-12-11 02:48

    BufferedReader does not accept FileInputStreams

    No, you cannot find out the line terminator character that was used in the file being read by BufferedReader. That information is lost while reading the file.

    Unfornunately all answers below are incorrect.

    Edit: And yes you can always extend BufferedReader to include the additional functionality you desire.

    0 讨论(0)
  • 2020-12-11 02:48

    If you are using groovy, you can simply do:

    def lineSeparator = new File('path/to/file').text.contains('\r\n') ? '\r\n' : '\n'
    
    0 讨论(0)
提交回复
热议问题