Java读取大文件
import java.io.*;
import java.nio.charset.StandardCharsets;
public class ReadBigFile {
public static void main(String[] args) throws IOException {
String path = "C:\\Users\\Administrator\\Desktop\\_posts\\java-basis.md";
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(path)));
BufferedReader reader = new BufferedReader(new InputStreamReader(bis, StandardCharsets.UTF_8),5*1024*1024);
String line = "";
while((line = reader.readLine()) != null){
if (line.contains("java")){
System.out.println(line);
}
}
reader.close();
bis.close();
}
}
注意在实例化BufferedReader时,增加一个分配缓存的参数即可