我试着在不移动指针的情况下读下一行,可以吗?
BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
while ((readString = buf.readLine()) != null) {
}
While将根据我的需要逐行读取,但我需要写入当前行的下一行。
有可能吗?
我的文件包含Http请求数据,第一行是GET请求,
在主机名的第二行中,我需要在GET之前拔出主机,以便能够将其连接在一起。
Host/+ GET url,
GET /logos/2011/family10-hp.jpg HTTP/1.1
Host: www.google.com
Accept-Encoding: gzip
谢谢。
发布于 2011-02-05 02:28:56
您可以使用mark()
和reset()
标记流中的一个点,然后返回到它。示例:
int BUFFER_SIZE = 1000;
buf.mark(BUFFER_SIZE);
buf.readLine(); // returns the GET
buf.readLine(); // returns the Host header
buf.reset(); // rewinds the stream back to the mark
buf.readLine(); // returns the GET again
发布于 2011-02-05 02:28:36
只需读取循环中的当前行和下一行。
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(file, encoding));
for (String next, line = reader.readLine(); line != null; line = next) {
next = reader.readLine();
System.out.println("Current line: " + line);
System.out.println("Next line: " + next);
}
} finally {
if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
}
https://stackoverflow.com/questions/4901508
复制相似问题