public static void main(String[] args) throws IOException {
File file = new File("G:\\a\\d.java");
FileInputStream fileInputStream = new FileInputStream(file);
int read=0 ;
while ((read = fileInputStream.read())!=-1){
System.out.print((char) read);
}
fileInputStream.close();
}
提高效率,指定一次读的数量
// 读取硬盘上的文件
public static void main3(String[] args) throws IOException {
// File file = new File("G:\\a\\d.java");
FileInputStream fileInputStream = new FileInputStream("G:\\a\\d.java");
byte[] bytes = new byte[2];
// 一次性把数据读入字节数组
// int read1 = fileInputStream.read(bytes);
int total=0;
while ((total=fileInputStream.read(bytes))!=-1){
System.out.print(new String(bytes,0,total));
}
// int read=0 ;
// while ((read = fileInputStream.read())!=-1){
// System.out.print((char) read);
// }
// 一定要记得关流,释放资源
fileInputStream.close();
}