方法1:定义txt内容同等大小的数组,一次性读出文本的所有内容。
File file = new File("D:\\xx.txt");
Long fileLengthLong = file.length();
byte[] fileContent = new byte[fileLengthLong.intValue()];
try {
FileInputStream inputStream = new FileInputStream(file);
inputStream.read(fileContent);
inputStream.close();
} catch (Exception e) {
// TODO: handle exception
}
String string = new String(fileContent);
方法2:按行循环读取文本内容.
BufferedReader in = new BufferedReader(new FileReader("D:\\xx.txt"));
String line = null;
while((line = in.readLine())!=null)
{
System.out.println(line);
}