JAVA——字节流: InputStream /OutputStream

本文深入讲解Java中的IO流操作,包括字节流和字符流的基本使用方法,如FileInputStream、FileOutputStream等,并通过实例演示如何利用这些流复制图片和MP3文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
字符流:
FileReader
FileWriter

BUfferedReader
BufferedWriter

字节流:
InputStream  OutputStream
如果想要操作一个图片数据,这时就要想到字节流。
*/
import java.io.*;
class FileStream
{
    public static void main(String[] args)throws IOException
    {
        readFile_3();
    }
    public static void readFile_3()throws IOException
    {
        FileInputStream fis = new FileInputStream("fos.txt");
        int num = fis.available();
        byte[] buf = new byte[num];//定义一个刚刚好的缓冲区。不用再循环了。
        fis.read(buf);
        System.out.println(new String(buf));
        fis.close();
    }
    public static void readFile_2()throws IOException
    {
        FileInputStream fis = new FileInputStream("fos.txt");
        byte[]buf = new byte[1024];
        int len = 0;
        while((len = fis.read(buf))!=-1)
        {
            System.out.println(new String(buf,0,len));
        }
        fis.close();
    }
    public static void readFile_1()throws IOException
    {
        FileInputStream fis = new FileInputStream("fos.txt");
        int ch = 0;
        while((ch=fis.read())!=-1)
        {
            System.out.println((char)ch);
        }
        fis.close();
    }
    public static void writeFile()throws IOException
    {
        FileOutputStream fos = new FileOutputStream("fos.txt");
        fos.write("abcde".getBytes());
        fos.close();
    }
}

下面举例实现将一个图片复制一份。

复制一个图片

(1)用字节读取流对象和图片关联。
(2)用字节写入流对象创建一个图片文件。用于存储获取到的图片数据
(3)通过循环读写,完成数据的存储
(4)关闭资源

/*
字符流只是拿来处理文字数据,对于媒体数据,我们使用字节流;
*/
import java.io.*;
class CopyPic
{
    public static void main(String[] args)
    {
        FileOutputStream fos = null;
        FileInputStream fis = null;
        try
        {
            fos = new FileOutputStream("two.png");
            fis = new FileInputStream("one.png");
            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("写入关闭失败");
            }
        }
    }
}

复制一个mp3文件

//自定义的读取方法
import java.io.*;
class MyBufferedInputStream
{
    private InputStream in;
    private byte[] buf = new byte[1024*4];
    private int pos = 0,count = 0;
    MyBufferedInputStream(InputStream in)
    {
        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&0xff;
        }
        else if(count>0)
        {
            byte b = buf[pos];
            count--;
            pos++;
            return b&0xff;
        }
        return -1;
    }
    public void myClose()throws IOException
    {
        in.close();
    }
}
/*
演示mp3的复制,通过缓冲区
BufferedOutputStream
BufferedInputStream
*/

import java.io.*;
class CopyMp3
{
    public static void main(String[] args)throws IOException
    {
        long start = System.currentTimeMillis();
        copy_2();
        long end = System.currentTimeMillis();
        System.out.println((end-start)+"毫秒");
    }
    public static void copy_2()throws IOException
    {
        MyBufferedInputStream bufis = new MyBufferedInputStream(new         FileInputStream("1.mp3"));
        BufferedOutputStream bufos = new BufferedOutputStream(new 
        FileOutputStream("2.mp3"));
        int by = 0;
        while((by=bufis.myRead())!=-1)
        {
            bufos.write(by);
        }
        bufos.close();
        bufis.myClose();
    }
    //通过字节流的缓冲区完成复制。
    public static void copy_1()throws IOException
    {

        BufferedInputStream bufis = new BufferedInputStream(new         FileInputStream("1.mp3"));
        BufferedOutputStream bufos = new BufferedOutputStream(new 
        FileOutputStream("2.mp3"));
        int by = 0;
        while((by= bufis.read())!=-1)
        {
            bufos.write(by);
        }
        bufis.close();
        bufos.close();

    }
}
/*
11111111-111111110000000000101001001010100101010010101001010

byte: -1  --->  int : -1;
00000000 00000000 00000000 11111111  255

11111111 11111111 11111111 11111111


11111111  -->提升了一个int类型 那不还是-1吗?是-1的原因是因为在8个1前面补的是1导致的。
那么我只要在前面补0,即可以保留原字节数据不变,又可以避免-1的出现。
怎么补0呢?

 11111111 11111111 11111111 11111111                        
&00000000 00000000 00000000 11111111 
------------------------------------
 00000000 00000000 00000000 11111111 

0000-0001
1111-1110
000000001
1111-1111  -1
*/

结论

字节流的读一个字节的read方法为什么返回值类型不是byte,而是int?
因为有可能会读到连续8个二进制1的情况,8个二进制1对应的十进制是-1.
那么就会数据还没有读完,就结束的情况。因为我们判断读取结束是通过结尾标记-1来确定的。
所以,为了避免这种情况将读到的字节进行int类型的提升。
并在保留原字节数据的情况前面了补了24个0,变成了int类型的数值。
而在写入数据时,只写该int类型数据的最低8位。

运行结果:
21毫秒

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值