BufferedReader(Java)能在不移动指针的情况下读取下一行吗?
BufferedReader(Java)能在不移动指针的情况下读取下一行吗?,java,Java,我试图在不移动指针的情况下阅读下一行,这可能吗 BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS)); while ((readString = buf.readLine()) != null) { } While将根据我的需要逐行读取,但我需要写入当前行的下一行 有可能吗 我的文件包含Http请求数据,第一行是GET请求, 在主机名的第二行中,我需要先拔出主机,然后才能将其连接到一起。 Host/+获取
我试图在不移动指针的情况下阅读下一行,这可能吗
BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
while ((readString = buf.readLine()) != null) {
}
While将根据我的需要逐行读取,但我需要写入当前行的下一行
有可能吗
我的文件包含Http请求数据,第一行是GET请求,在主机名的第二行中,我需要先拔出主机,然后才能将其连接到一起。
Host/+获取url
GET /logos/2011/family10-hp.jpg HTTP/1.1
Host: www.google.com
Accept-Encoding: gzip
谢谢。只要阅读循环中的当前行和下一行即可
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) {}
}
您可以使用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
谢谢你,巴卢斯!不要认为这只是eclipse—但我必须将“for”代码行修改为
for(String next=”,line=reader.readLine();line!=null;line=next){
,以解决“局部变量next可能尚未初始化”的编译器问题完美的解决方案!!它解决了我的读取文件问题,我必须在下一行执行一些逻辑,然后返回到上一行设置条件。