IO-字节流的使用

1.用字节流拷贝图片文件;

2.用字节流缓冲区拷贝图片文件;      ----------------------------------->对应下面的一个copy_pic( )函数

3.自己实现一个字节流缓冲区并实现上述功能;------------------->对应下面的一个mycopy_pic( )函数


打开图片文件的命令:  display    [图片文件名]


import java.io.*;

public class CopyPicture
{
    public static void main(String[] args)
    {
        FileOutputStream fos = null;
        FileInputStream fis = null;
        try
        {
            fos = new FileOutputStream("/home/cjn/文档/Java_Learn/IO文件/copyMyGoddess.jpeg");
            fis = new FileInputStream("/home/cjn/下载/picture/my_goddess.jpeg");

            byte[] buf = new byte[1024];
            int len = 0;

            while((len=fis.read(buf))!=-1)
            {
                fos.write(buf,0,len);
            }
        }
        catch(IOException e)
        {
            throw new RuntimeException("复制文件失败!");
        }
         finally
        {
            try
            {
                if(fis != null)
                    fis.close();
            }
            catch(IOException e)
            {
                throw new RuntimeException("读取关闭失败");
            }
            try
            {
                if(fos != null)
                    fos.close();
            }
            catch(IOException e)
            {
                throw new RuntimeException("写入关闭失败");
            }
        }
    }
}
      

 

2和3 的代码:

对于字节流缓存区的一个图例解释:(方便后面写自定义的代码)


import java.io.*;
class MyBufferStream
{
    private InputStream in;
    private byte[] buf = new byte[1024];

    private int pos = 0,count = 0;

    MyBufferStream(InputStream in) throws IOException
    {
        this.in = in;
    }
    //一次读一个字节,从缓冲区(字节数组)获取
    public int myRead() throws IOException
    {
       //通过in对象读取硬盘上数据,并存储buf中.
       if(count == 0)
       {
          count = in.read(buf);
          if(count < 0)
             return -1;
          pos = 0; 
          byte b = buf[pos];
          count--;
          pos++; 
          return b;
        }
        else if(count > 0)                                                            
        {                                                                                  
           byte b = buf[pos];
           count--;
           pos++; 
           return b;
        }
        return -1;
    }
    public void myClose() throws IOException
    {
        in.close();
    }
}

public class BufCopyPic
{
    public static void main(String[] argv) throws IOException
    {
        long start = System.currentTimeMillis();
        mycopy_pic();
        long end = System.currentTimeMillis();
        System.out.println(end-start+"毫秒");
    }
    public static void mycopy_pic() throws IOException
    {
        MyBufferStream bufis = new MyBufferStream(new FileInputStream("/home/cjn/下载/picture/my_goddess.jpeg"));
        BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("/home/cjn/文档/Java_Learn/IO文件/mybufCopyMyGoddess.jpeg"));

        int by = 0;

        System.out.println("第一个字节:"+bufis.myRead());
        while((by=bufis.myRead()) != -1)
        {
             bufos.write(by);
        }
        bufos.close();
        bufis.myClose();
    }

    public static void copy_pic() throws IOException
    {
        BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("/home/cjn/下载/picture/my_goddess.jpeg"));
        BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("/home/cjn/文档/Java_Learn/IO文件/bufCopyMyGoddess.jpeg"));

        int by = 0;
        while((by=bufis.read()) != -1)
        {
            bufos.write(by);
        }

        bufos.close();
        bufis.close();
    }
}


 

如果将以上代码进行运行那么可以看到一下结果:


图片文件打不开!!!

究其原因,我们打印下从图片文件度过来的数据:


读出来竟然是-1!!!

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

图片文件都是以0 1 二进制码存起来的,所以我们用byte去读的时候,

比如:文件为:111111110000110101010101010101

byte去读的时候候11111111--------->提升为int 为 11111111  11111111  11111111  11111111

但是为什么要用int 接受而不用byte接受???

因为用int去接收可以使得提升前面补0而不是补1;

也就是提升为:0000000    00000000    0000000   11111111

保证了原数据不变而且不是-1;

因此我们就只取后面八位,所以就要与上255也就是0xff !

修改的代码如下:

import java.io.*;
class MyBufferStream
{
    private InputStream in;
    private byte[] buf = new byte[1024];

    private int pos = 0,count = 0;

    MyBufferStream(InputStream in) throws IOException
    {
        this.in = in;
    }
    //一次读一个字节,从缓冲区(字节数组)获取
    public int myRead() throws IOException
    {
       //通过in对象读取硬盘上数据,并存储buf中.
       if(count == 0)
       {
          count = in.read(buf);
          if(count < 0)
             return -1;
          pos = 0; 
          byte b = buf[pos];
          count--;
          pos++; 
          return b&255;
        }
        else if(count > 0)                                                            
        {                                                                                  
           byte b = buf[pos];
           count--;
           pos++; 
           return b&0xff;
        }
        return -1;
    }
    public void myClose() throws IOException
    {
        in.close();
    }
}

运行结果:


但是还是打不开:

要把上边那句打印第一个字节的删掉,因为打印的时候没有读进去!!!



end...


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值