BufferedReader类 使用了这个类的函数readLine()读到文件结尾时想继续用readLine()重新从文件头开始读,应该怎么把指针放到文件开始位置 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用RandomAccessFile类随机读写可以吧RandomAccessFile rf = new RandomAccessFile(File file, "r");rf.seek(0); 要随机地读的话请用RandomAccessFile类,若你实在要用BufferedReader的话,那你就关闭一次再打开一次好了,那就如一楼所说,闲得没事了……可能楼主有了解C读文件,什么fseek之类的,你要的这些在RandomAccessFile类中有。 不太熟,先然后reset也许可以,毕竟不清楚是否实现了这个功能,但他要做了也很容易操作public void (int readAheadLimit) throws IOExceptionMarks the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point. 如果只是学习一下或者针对小文件的话: File file = new File("D:/test/test.txt"); BufferedReader reader = null; int buffSize = 4096; try { reader = new BufferedReader(new FileReader(file), buffSize); String line; reader.(((int) file.length()) + buffSize); while ((line = reader.readLine()) != null) { System.out.println(line); } reader.reset(); System.out.println("============================="); System.out.println(reader.readLine()); } catch (Exception ex) { ex.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (Exception ex) { } } }针对性能要求高的程序,或者大文件,直接使用上面的RandomAccessFile 重点就在这个readAheadLimit要考虑的buffer的大小 RandomAccessFile 直接使用这个吧 你把以前的bufferedReader指针close 然后再次new BufferedReader 也相当于回溯到文件头了 close之前的BufferReader 然后再new BufferReader 就回溯到文件头 outputstream的问题,请教 难道上海的工资比深圳高很多吗 经典作品--Java解惑谜题连载① java的数据库语句(急在线等) 2008年应届生求职java相关工作 关于Vector类 在Java中能够使用ActiveX控件吗 如何实现动态指定List 泛型 实现自动化查询 已编译成功的程序为什么还会说“初始化错误”呢? 高手们,你们快来看看!!!!!! java xml 生成报错 Java/XMPP/GTalk/MSN
RandomAccessFile rf = new RandomAccessFile(File file, "r");
rf.seek(0);
再打开一次好了,那就如一楼所说,闲得没事了……
可能楼主有了解C读文件,什么fseek之类的,你要的这些在RandomAccessFile类中有。
public void (int readAheadLimit)
throws IOException
Marks the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point.
BufferedReader reader = null;
int buffSize = 4096;
try {
reader = new BufferedReader(new FileReader(file), buffSize);
String line;
reader.(((int) file.length()) + buffSize);
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.reset();
System.out.println("=============================");
System.out.println(reader.readLine());
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception ex) {
}
}
}针对性能要求高的程序,或者大文件,直接使用上面的RandomAccessFile
你把以前的bufferedReader指针close 然后再次new BufferedReader 也相当于回溯到文件头了