我试图在不移动指针的情况下阅读下一行,这可能吗?
BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
while ((readString = buf.readLine()) != null) {
}
Run Code Online (Sandbox Code Playgroud)
虽然我会想要逐行阅读,但我需要写下当前行的下一行.
有可能的?
我的文件包含Http请求数据,第一行是GET请求,
在第二行是主机名,我需要在GET之前拉出主机才能将它们连接在一起.
主持人/ + GET网址,
GET /logos/2011/family10-hp.jpg HTTP/1.1
Host: www.google.com
Accept-Encoding: gzip
Run Code Online (Sandbox Code Playgroud)
谢谢.
ata*_*lor 46
您可以使用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
Run Code Online (Sandbox Code Playgroud)
Bal*_*usC 15
只需读取循环中的当前行和下一行.
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) {}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34160 次 |
| 最近记录: |