package zmx.Io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class demo05 {
public static void main(String[] args){
//1、建立联系 File对象
File src=new File("F:"+File.separator+"记事"+File.separator+"5.English.docx ");
//2、选择流
InputStream is=null;
try {
is=new FileInputStream(src);
//3.操作不断读取
byte[] car=new byte[10];
int len=0;//接收实际读取大小
//循环读取
while(-1!=(len=is.read(car))){
//输出字节数组转成字符串
String info=new String(car,0,len);
System.out.println(info);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("文件不存在");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("读取文件失败");
}
finally {
if(null!=is){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("关闭文件输入流资源失败");
}
}
}
}
}
InputStream文件输入流
最新推荐文章于 2025-03-13 22:01:42 发布