读一个文本,第一次用readLine()之后,再用一次readLIne()读同样文本,它从第一次的行标记继续往下读,怎么让它从文件的第一行重新开始呀???

解决方案 »

  1.   

    java.io
    类 LineNumberReaderjava.lang.Object
      继承者 java.io.Reader
          继承者 java.io.BufferedReader
              继承者 java.io.LineNumberReader所有已实现的接口:
        Closeable, Readablepublic class LineNumberReader
    extends BufferedReader跟踪行号的缓冲字符输入流。此类定义方法 void setLineNumber(int) 和 int getLineNumber(),它们可分别用于设置和获取当前行号。默认情况下,行编号从 0 开始。该行号随数据读取递增,并可以通过调用 setLineNumber(int) 进行更改。但要注意,setLineNumber(int) 不会实际更改流中的当前位置;它只更改将由 getLineNumber() 返回的值。可认为行是由换行符('\n')、回车符('\r')或回车后面紧跟换行符中的任何一个终止的。从以下版本开始:
        JDK1.1字段摘要
     
    从类 java.io.Reader 继承的字段
    lock
     
    构造方法摘要
    LineNumberReader(Reader in)
              使用默认输入缓冲区的大小创建新的行编号 reader。
    LineNumberReader(Reader in, int sz)
              创建新的行编号 reader,将字符读入给定大小的缓冲区。
     
    方法摘要
     int  getLineNumber()
              获得当前行号。
     void  (int readAheadLimit)
              标记该流中的当前位置。
     int  read()
              读取单个字符。
     int  read(char[] cbuf, int off, int len)
              将字符读入数组中的某一部分。
     String  readLine()
              读取文本行。
     void  reset()
              将该流重新设置为最新的标记。
     void  setLineNumber(int lineNumber)
              设置当前行号。
     long  skip(long n)
              跳过字符。
     
    从类 java.io.BufferedReader 继承的方法
    close, Supported, ready
     
    从类 java.io.Reader 继承的方法
    read, read
     
    从类 java.lang.Object 继承的方法
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     构造方法详细信息
    LineNumberReaderpublic LineNumberReader(Reader in)    使用默认输入缓冲区的大小创建新的行编号 reader。    参数:
            in - 提供基础流的 Reader 对象。LineNumberReaderpublic LineNumberReader(Reader in,
                            int sz)    创建新的行编号 reader,将字符读入给定大小的缓冲区。    参数:
            in - 提供基础流的 Reader 对象。
            sz - 指定缓冲区大小的 int。方法详细信息
    setLineNumberpublic void setLineNumber(int lineNumber)    设置当前行号。    参数:
            lineNumber - 指定行号的 int。
        另请参见:
            getLineNumber()getLineNumberpublic int getLineNumber()    获得当前行号。    返回:
            当前行号。
        另请参见:
            setLineNumber(int)readpublic int read()
             throws IOException    读取单个字符。行结束符被压缩为单一新行('\n')字符。    覆盖:
            类 BufferedReader 中的 read    返回:
            读取的字符,如果已到达流的末尾,则返回 -1 
        抛出:
            IOException - 如果发生 I/O 错误readpublic int read(char[] cbuf,
                    int off,
                    int len)
             throws IOException    将字符读入数组中的某一部分。    覆盖:
            类 BufferedReader 中的 read    参数:
            cbuf - 目标缓冲区
            off - 开始存储字符处的偏移量
            len - 要读取的最多字符数 
        返回:
            读取的字节数量,如果已到达流的末尾,则返回 -1 
        抛出:
            IOException - 如果发生 I/O 错误readLinepublic String readLine()
                    throws IOException    读取文本行。可认为行是由换行符('\n')、回车符('\r')或回车后面紧跟换行符中的任何一个终止的。    覆盖:
            类 BufferedReader 中的 readLine    返回:
            包含行的内容且不包括任何行结束符的字符串,如果已到达流的末尾,则返回 null 
        抛出:
            IOException - 如果发生 I/O 错误skippublic long skip(long n)
              throws IOException    跳过字符。    覆盖:
            类 BufferedReader 中的 skip    参数:
            n - 要跳过的字符数 
        返回:
            实际跳过的字符数 
        抛出:
            IOException - 如果发生 I/O 错误 
            IllegalArgumentException - 如果 n 为负public void (int readAheadLimit)
              throws IOException    标记该流中的当前位置。对 reset() 的后续调用将尝试重新将流定位到此点,还将相应地重新设置行号。    覆盖:
            类 BufferedReader 中的     参数:
            readAheadLimit - 在仍保留该标记的情况下,对可读取字符数量的限制。在读取这样多的字符后,尝试重置流可能会失败。 
        抛出:
            IOException - 如果发生 I/O 错误resetpublic void reset()
               throws IOException    将该流重新设置为最新的标记。    覆盖:
            类 BufferedReader 中的 reset    抛出:
            IOException - 如果该流未标记,或者该标记已失效
      

  2.   

    根据LZ的需求 你可以通过这个类来完成 运气真好 刚打开API 就找到了。。
      

  3.   

    简单的读一般如下,while ( (var = xx.readLine()) != null )
    {
            sysout("var");
    }
    至于你的问题。明确需求!—*( !·#¥%……
      

  4.   


    * 文件随机访问的方法
     *
     * void seek(long pos); 将文件指针移动到参数指定的位置。
     * long getFilePointer(); 得到当前文件指针的位置。
     * int skipBytes(int n); 将文件指针向前移动参数指定的n个字节。
     * String readLine(); 从当前文件指定位置读取一行。
     *
     */
    public class RandomFile
    {
        public static void main(String[] args)
        {
            String tempStr;
            int fileLines = 0;
            long pointerLast = 0;
            try
            {
                RandomAccessFile inObj = new RandomAccessFile("d:\\mydir\\secondFile.txt", "rw");
                while (inObj.readLine() != null)
                    fileLines++;
                for (int i =0; i < fileLines / 2; i++)
                {
                    inObj.seek(2 * i);
                    tempStr = inObj.readLine();
                    System.out.println(tempStr);
                }
                pointerLast = inObj.getFilePointer();
            }catch(IOException e)
            {
                e.printStackTrace();
            }
            try
            {
                RandomAccessFile fileObj = new RandomAccessFile("d:\\mydir\\secondFile.txt", "rw");
                String writeStr = new String("Insert a string!");
                fileObj.seek(pointerLast);
                fileObj.writeChars(writeStr);
            }catch(IOException e)
            {
                e.printStackTrace();
            }
        }
    }
      

  5.   

    public class LineNumberReader
    extends BufferedReader跟踪行号的缓冲字符输入流。此类定义方法 void setLineNumber(int) 和 int getLineNumber(),它们可分别用于设置和获取当前行号。默认情况下,行编号从 0 开始。该行号随数据读取递增,并可以通过调用 setLineNumber(int) 进行更改。