Java基础-IO流中FileInputStream

FileInputStream继承了InputStream,被称为文件字节输入流

构造方法

  • FileInputStream(File file)
    通过打开与实际文件的连接创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。
  • FileInputStream(String name)
    通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。
  • 如果文件不存在或者不是文件是目录、则会抛出FileNotFoundException异常

FileInputStream常用方法

  • public int read() throws IOException
    从该输入流读取一个字节的数据。如果没有输入可用,此方法将阻止
  • public int read(byte[] b)throws IOException
    从该输入流读取最多b.length字节的数据到字节数组。 此方法将阻塞,直到某些输入可用。
  • public int read(byte[] b, int off,int len) throws IOException
  • 从该输入流读取最多len字节的数据为字节数组。 如果len不为零,该方法将阻塞,直到某些输入可用; 否则,不会读取字节,并返回0 。

实战构建

public class FileInputStreamDemo {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null ;
       try {
           fileInputStream = new FileInputStream(".\\src\\IOTEST\\TEST93");
           byte[] bytes = new byte[1024] ;
           int len = 0 ;
           while((len = fileInputStream.read(bytes))!=-1){
               System.out.println(new String(bytes,0,len));
           }
       }catch (FileNotFoundException e){
           e.printStackTrace();
       }catch (IOException e){
           e.printStackTrace();
       }finally {
           if(fileInputStream != null){
               try {
                   fileInputStream.close();
               }catch (IOException  e){
                   e.printStackTrace();
               }
           }
       }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值