import java.io.*;
import java.math.BigInteger;
public class ReadTest {
public static void readBigFile(String path) {
long start = System.currentTimeMillis();//开始时间
BigInteger sum;//记录行数
try {
File file = new File(path);//打开文件
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));
BufferedReader reader = new BufferedReader(new InputStreamReader(fis), 10 * 1024 * 1024);// 用10M的缓冲读取文本文件
String line;//记录每行内容
sum = BigInteger.ZERO;//记录行数
while ((line = reader.readLine()) != null) {
sum = sum.add(BigInteger.ONE);
System.out.println("No."+sum+"---"+line);//打印每行内容
}
System.out.println("文件总共有"+sum+"行");
long end = System.currentTimeMillis();//结束时间
System.out.println("总共耗时:"+(end - start)+"ms");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
readBigFile("C:\\a.txt");//文件位置
}
}