InputStream(Java)

InputStream类讲解FileInputStream类、BufferedInputStream类

InputStream 之 FileInputStream类:文件字节输入流

// 使用int read()方法读取文件

try {

    InputStream fis = new FileInputStream("F:\\InputStreamDemo.txt");	
    // InputStream fis = new FileInputStream(new File("F:\\InputStreamDemo.txt"));

    int i = 0;
    while (i != -1) { 
        i = fis.read(); // 返回值为-1读取完毕
        System.out.print((char)i); // read返回值为unicode编码,强制转为字符
    }

    fis.close(); // 释放资源

    } catch (FileNotFoundException e) { // InputStream
        e.printStackTrace();
    } catch (IOException e) { // read()
        e.printStackTrace();
    }

/*
解释:
1、FileInputStream有两种传参方式,可以传文件地址,也可以传一个文件对象
2、read()方法读取输入流数据的一个字节,并返回该字节对应的Unicode编码,当读取到文件末尾时返回-1
3、读取结束一定要使用close方法释放与该流关联的资源
4、InputStream和read()各需要一个catch
*/
// int read(byte c[]) 读取文件

try {

    // InputStream fis = new FileInputStream(new File("F:\\InputStreamDemo"));
    InputStream fis = new FileInputStream("F:\\InputStreamDemo.txt");
    
    byte[] buffer = new byte[10]; // 创建以字节为单位的数组,buffer大小为10个字节
    int len = fis.read(buffer);   // 一次读取十个字节的数据,返回的是读取的数据长度,返回值为-1时读取完毕
			
    while(len != -1) {
        System.out.print(new String(buffer, 0, len)); // 转换为字符
        len = fis.read(buffer);
    }

    fis.close(); // 释放资源		
			
    } catch (FileNotFoundException e) { // InputStream
        e.printStackTrace();
    } catch (IOException e) { // read
        e.printStackTrace();
    }

/*
解释:
1、我们可以自定义一次读取数据的长度,就需要设置自定义大小的字节数组buffer
2、read(buffer)方法读取的是b数组字节长度的数据,并将其数据存到buffer数组中
   read(buffer)返回值为读取的数据长度,返回值为-1时表示读取完毕
   如果创建的buffer长度为10个字节,而文件只有3个字节,那么read(buffer)返回的是3
3、想要获得buffer中数据就要将其中的类型转换为字符,用到String类中的
   String(byte bytes[], int offset, int length)方法
   第一个参数为存储数据的数组,第二个参数为从数组哪里开始转换,第三个参数为要转换的长度
*/

InputStream 之 BufferedInputStream类:带缓冲的字节输入流

// 使用BufferedInputStream读取文件

try {
			
    InputStream fis = new FileInputStream("F:\\StreamDemo.txt"); 
    BufferedInputStream bis = new BufferedInputStream(fis);
    // new BufferedInputStream时要传入InputStream的类型的类,这是将InputStream流封装

    byte[] buffer = new byte[10];
    int len = 0; 

    // while((len = bis.read(buffer, 0, 1)) != -1)) 另一种read方法
    while((len = bis.read(buffer)) != -1) {
        System.out.println(new String(buffer, 0, len));
        System.out.println("每次读取字节数量:" + len);
        System.out.println("文件中剩余字节数:" + bis.available());
    }   
 
    bis.close();
    fis.close();	
		
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

/*
解释:
阅读read(buffer, 0, 1)源码发现其方法原形:read(byte b[], int off, int len)
    - 字节数组b是目标缓冲区,也就是将读取的数据存在b中
    - off是开始存储字节的偏移量,也就是读取的数据在存进b时,off为0就紧接b中数据存储,off为1就空出一个位置再存储
    - len是每次读取最大字节数
*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值