使用了这个类的函数readLine()读到文件结尾时想继续用readLine()重新从文件头开始读,应该怎么把指针放到文件开始位置

解决方案 »

  1.   

    用RandomAccessFile类随机读写可以吧
    RandomAccessFile rf = new RandomAccessFile(File file, "r");
    rf.seek(0);
      

  2.   

    要随机地读的话请用RandomAccessFile类,若你实在要用BufferedReader的话,那你就关闭一次
    再打开一次好了,那就如一楼所说,闲得没事了……
    可能楼主有了解C读文件,什么fseek之类的,你要的这些在RandomAccessFile类中有。
      

  3.   

    不太熟,先然后reset也许可以,毕竟不清楚是否实现了这个功能,但他要做了也很容易操作
    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. 
      

  4.   

    如果只是学习一下或者针对小文件的话:    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
      

  5.   

    重点就在这个readAheadLimit要考虑的buffer的大小
      

  6.   

    RandomAccessFile 直接使用这个吧
      

  7.   


    你把以前的bufferedReader指针close 然后再次new BufferedReader  也相当于回溯到文件头了
      

  8.   

    close之前的BufferReader  然后再new BufferReader  就回溯到文件头