各位大虾,我想请教一下,java 读取txt 文件时,是一次性读入内存然后一行一行取呢?还是一行一行来读的?

2024-12-15 00:21:36
推荐回答(3个)
回答1:

如果你选择的是文本输入流,是一行一行的读。

回答2:

FileReader :把文件转换为字符流读入;

BufferedReader会一次性从物理流中读取8k(默认数值,可以设置)字节内容到内存

String line = br.readLine();然后一行一行取

具体看下面网址

回答3:

/**
*读取指定路径的文件,按行读取
*
*/
public static String readTextFile(String path) throws Exception {
//String ret = new String();
StringBuffer ret = new StringBuffer();
//BufferedReader in = new BufferedReader(new FileReader(path));
BufferedReader in =
new BufferedReader(
new InputStreamReader(new FileInputStream(path), "gb2312"));

String s;// = new String();
//按行读取
while ((s = in.readLine()) != null) {

ret.append(s);
}
in.close();

return ret.toString();
}