方法一:
注意:需要判断文件是否存在,否则会报空指针错误。
import java.io.File;
import java.io.FileInputStream;
public class FileTest {
public static void main(String[] args) {
FileInputStream fis = null;
try {
File file = new File(“D://test.txt”);
fis = new FileInputStream(file);
byte[] b = new byte[(int)file.length()];
while (fis.read(b) != -1) {
}
System.out.println(new String(b));
} catch (Exception e) {
e.printStackTrace();
}
}
}
方法二:
FileReader fr =new FileReader("…txt");
BufferedReader br=new BufferedReader(fr);
StringBuffer sb = new StringBuffer();
String str = null;
while ((str = br.readLine()) != null)
sb.append(str);