比如说我读一个有1000行的txt文件能不能直接选择第500行读取?
我找了一下api貌似没有这样的方法啊?莫非只能一行一行的读?读到500?
我找了一下api貌似没有这样的方法啊?莫非只能一行一行的读?读到500?
解决方案 »
- Java中线程池中断子线程的问题。有代码,有经验的来看看。
- 求助关于内部类的问题..这个有什么错?大家帮下忙.谢谢!!
- 求教!!!!像一般IDE我们写了一个方法,但未使用过,他会在下面做个下划线标识一下,这个是怎么实现的??
- 小弟初学,读程序时有几地方看不民懂,?的地方请大家帮忙解答,谢谢,在线等!!!!!
- j2sdk1.4.2_04安装包安装了不能用??
- 求高手进来,菜鸟数据库连接问题
- [ChangeAwareClassLoader]什么意思?
- 我刚刚开始学J++Builder,请问各位大侠能否为小弟推荐几本好书呀!(在线等待 分值:100)
- 这里面有没有北京的,喜欢学习Java小弟想共同探讨
- Java小白,使用Java编写一个猜字游戏
- static块和mian函数
- 关于List的isEmpty()和size()方法的疑惑?
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.
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行的前提就是你文本有换行,不要一大段写下来就一行!
再就如果想读五百行的话
int i=0;
BufferedReader br = .....;
Stirng s = br.readLine();
while(s!=null){
i++;
s = br.readLine();
if(i==500)
break;
}
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(); }}