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

后端 未结 9 1399
星月不相逢
星月不相逢 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:52

    Maybe you could use Scanner instead.

    You can pass regular expressions to Scanner#useDelimiter() to set custom delimiter.

    String regex="(\r)?\n";
    String filename=....;
    Scanner scan = new Scanner(new FileInputStream(filename));
    scan.useDelimiter(Pattern.compile(regex));
    while (scan.hasNext()) {
        String str= scan.next();
        // todo
    }
    

    You could use this code below to convert BufferedReader to Scanner

     new Scanner(bufferedReader);
    
    0 讨论(0)
  • 2020-12-11 02:54

    After reading the java docs (I confess to being a pythonista), it seems that there isn't a clean way to determine the line-end encoding used in a specific file.

    The best thing I can recommended is that you use BufferedReader.read() and iterate over every character in the file. Something like this:

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

    If you happen to be reading this file into a Swing text component then you can just use the JTextComponent.read(...) method to load the file into the Document. Then you can use:

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

    to get actual EOL string that was used in the file.

    0 讨论(0)
提交回复
热议问题