在bufferedReader中读取行

the*_*tna 5 java bufferedreader

来自javadoc

public String readLine()
            throws IOException

Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed. 
Run Code Online (Sandbox Code Playgroud)

我有以下类型的文字:

Now the earth was formless and empty.  Darkness was on the surface
of the deep.  God's Spirit was hovering over the surface
of the waters.
Run Code Online (Sandbox Code Playgroud)

我正在阅读以下内容:

 while(buffer.readline() != null){
       }
Run Code Online (Sandbox Code Playgroud)

但是,问题是它正在考虑换行前的字符串行.但是我想在字符串结束时考虑行..我该怎么办?

ami*_*mit 7

您可以使用a Scanner并使用设置自己的分隔符useDelimiter(Pattern).

请注意,输入分隔符是正则表达式,因此您需要提供正则表达式\.(您需要打破.正则表达式中字符的特殊含义)


Pet*_*rey 5

您可以一次读取一个字符,并将数据复制到StringBuilder

Reader reader = ...;
StringBuilder sb = new StringBuilder();
int ch;
while((ch = reader.read()) >= 0) {
    if(ch == '.') break;
    sb.append((char) ch);
}
Run Code Online (Sandbox Code Playgroud)