Java Scanner newline recognition

前端 未结 2 1905
我在风中等你
我在风中等你 2020-12-17 06:51

I can\'t find the documentation that specifies how a Scanner treats newline patterns by default. I want to read a file line by line and have the scanner be able to handle \\

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-17 07:22

    It is not documented (in Java 1.6) but the JDK code uses this regex to match a line break:

    "\r\n|[\n\r\u2028\u2029\u0085]"
    

    Here's a link to the source code: http://cr.openjdk.java.net/~briangoetz/7012540/webrev/src/share/classes/java/util/Scanner.java.html

    IMO, this ought to be specified, since Scanner's behavior wrt to line separators is different to (for example) BufferedReader's. (I've lodged a bug report ...)

提交回复
热议问题