比如说我读一个有1000行的txt文件能不能直接选择第500行读取?
我找了一下api貌似没有这样的方法啊?莫非只能一行一行的读?读到500?

解决方案 »

  1.   

    看看这个呢java.io 
    Class LineNumberReader
    java.lang.Object
      java.io.Reader
          java.io.BufferedReader
              java.io.LineNumberReaderAll Implemented Interfaces: 
    Closeable, Readable --------------------------------------------------------------------------------public class LineNumberReaderextends BufferedReaderA buffered character-input stream that keeps track of line numbers. This class defines methods setLineNumber(int) and getLineNumber() for setting and getting the current line number respectively. By default, line numbering begins at 0. This number increments at every line terminator as the data is read, and can be changed with a call to setLineNumber(int). Note however, that setLineNumber(int) does not actually change the current position in the stream; it only changes the value that will be returned by getLineNumber(). A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed. 
      

  2.   

    楼上正解,可以使用LineNumberReader 这个包装类进行读取,设置行号,然后开始读
      

  3.   

    貌似1楼的方法木有用吧,setLineNumber(int n)只是一个摆设吧。。还是不能直接读取某一行
      

  4.   

    Note however, that setLineNumber(int) does not actually change the current position in the stream; it only changes the value that will be returned by getLineNumber(). 好像只能一行一行的读,木有别的办法
      

  5.   

    那就自己写个类好了增加这么个方法就行了。或者重载ReadLine这个方法。。就ok了
      

  6.   

    嘿嘿,这么简单,答案我知道。import java.io.*;
    public class jiekou
    {


    public static void main(String[] args)
    {
    String neirong="";
    File file=new File("C:\\Users\\Administrator\\Desktop\\book.txt");
    try
    { FileReader du=new FileReader(file);
    BufferedReader huancun=new BufferedReader(du);

    int col=0;
    String temp;
    while((temp=huancun.readLine())!=null)
    {
    neirong+=temp;
      ++col;
      if(col==5)
      {
      break;
      }
    }

    }
    catch(Exception e)
    {
    System.out.println("读取异常,原因:"+e.getMessage());
    }


    System.out.println("该文档前5行的内容是:\n"+neirong);
    }
    }读取前5行的前提就是你文本有换行,不要一大段写下来就一行!
      

  7.   

    可以用RandomAccessFile类
    再就如果想读五百行的话
    int i=0;
    BufferedReader br = .....;
    Stirng s = br.readLine();
    while(s!=null){
        i++;
        s = br.readLine();
        if(i==500)
            break;
    }
      

  8.   

    package Message;import java.io.*;
    import java.util.Scanner;class LineNum{
    private Reader in= null;
    private LineNumberReader lnbr =null;
    public LineNum(){
    File f = new File("d:"+File.separator+"files\\2.txt");
    try {
    this.in = new FileReader(f);
    this.lnbr = new LineNumberReader(in);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    }
    public void readLineCont(){
    Scanner cin = new Scanner(System.in);
    System.out.print("please input LineNumber:");
    int num = cin.nextInt();
    String temp;
    int i=0;
    try {
    while((temp=this.lnbr.readLine())!=null){
    this.lnbr.setLineNumber(i++);
    if(this.lnbr.getLineNumber()==num-1){
    System.out.println("the "+num+" content is:"+temp);
    }
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    public LineNumberReader getLnbr(){
    return this.lnbr;
    }
    }public class ReadLineNumber { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    LineNum ln = new LineNum();
    ln.readLineCont(); }}