JavaSE 学习参考:IO流之字节流

  

字节流Stream操作单元是字节,按流的方向分为字节输入流InputStream和字节输出流OutputStream

   InputStream 

是所有字节输入流的父类,包含两个核心方法:

   int read() 从流中一次读取一个字节,返回类型虽然为四个字节的int型,实际上只填充最后一个字节,前三个都为0

   int read(byte[] buffer) 从流中连续读取多个可用字节,最不超过buffer.length中缓冲在buffer数组中,返回实际读取字节数量。

    OutputStream 

是所有字节输出流的父类,包含两个核心方法:

   void write(int n) 将参数最后一个字节输出到流中。

   void write(byte[] buffer,int offset,int length) 将缓冲在buffer数组的字节信息从索引offset开始连接取length个输出到流中。

  

本文以File作为输入和输出目标和源介绍文件字节输入流FileInputStreamFileOutputStream这两个流类来复制d:\a.jpge:\a.jpg

示例代码:

 

    public static void main(String[] args) {

     FileInputStream fis=null;

     FileOutputStream fos=null;

    

     try {

fis=new FileInputStream("d:\\a.jpg");

fos=new FileOutputStream("e:\\b.jpg");

int n=-1;

while((n=fis.read())!=-1){

fos.write(n);

}

catch (FileNotFoundException e) {

e.printStackTrace();

catch (IOException e) {

e.printStackTrace();

}finally{

try {

if(fos!=null){

fos.flush();

fos.close();

}

if(fis!=null)fis.close();

catch (IOException e) {

e.printStackTrace();

}

}

    

    }

}

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值