Java -- FileInputStream

public class Test_FileInputStream {
// 1.从硬盘存在的一个文件中,读取其内容到程序中,使用FileInputStream
// 2.创建一个file的对象
@Test
public void Test1() throws IOException{
// 1.创建一个File类的对象
File file = new File("C:\\Users\\Administrator\\Desktop\\heihei.txt");
// 2.创建一个FileInputStream类的对象
FileInputStream fis = new FileInputStream(file);
// 3.调用FileInputStream的方法,实现file文件的读取
/*
* read():读取文件的一个字节
*/
int b = fis.read();
while(b != -1){
System.out.print(b);
b = fis.read();
}
// 4.关闭相应的流
fis.close();
}
@Test
public void Test2(){
FileInputStream fis = null;
try{
File file = new File("C:\\Users\\Administrator\\Desktop\\heihei.txt");
int b = fis.read();
while(b != -1){
System.out.print(b);
b = fis.read();
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
// 4.关闭相应的流
fis.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值