第8章-第3节-Java中的字节流

1、字节输出流OutputStream(写出):

父类:OutputStream 是字节输出流的父类,该类是一个抽象类,不能直接实例化

子类:FileOutputStream 继承了OutputStream ,是一个具体的类

1)、构造方法:

方法名说明
public FileOutputStream(String name)创建一个字节输出流,指向指定的文件路径
public FileOutputStream(String name, boolean append)创建一个字节输出流,指向指定的文件路径,追加写内容
public FileOutputStream(File file)创建一个字节输出流,指向file文件
public FileOutputStream(File file, boolean append)创建一个字节输出流,指向file文件,追加写内容
File file = new File("test\\a1.txt");
OutputStream out = new FileOutputStream(file, true);
out.close();

 2)、写出数据:

方法名说明
void write(int b)将指定的字节写入此文件输出流 一次写一个字节数据
void write(byte[] b)将 b.length字节从指定的字节数组写入此文件输出流 一次写一个字节数组数据
void write(byte[] b, int off, int len)将 len字节从指定的字节数组开始,从偏移量off开始写入此文件输出流 一次写一个字节数组的部分数据
// 写出指定长度的字节数组
byte[] bytes = "我爱蜗牛".getBytes("UTF-8");
out.write(bytes, 3, bytes.length - 3);

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

 2、字节输入流InputStream(读入):

父类:InputStream 是字节输入流的父类,该类是一个抽象类,不能直接实例化

子类:FileInputStream 继承了InputStream ,是一个具体的类

1)、构造方法:

方法名说明
public FileInputStream(String name)创建一个字节输入流,指向指定的文件路径
public FileInputStream(File file)创建一个字节输入流,指向file文件
// 创建一个字节输入流,指向一个file文件
// 如果文件不存在,这会出现运行时异常
File file = new File("test\\a.txt");
if(!file.exists()){
    file.createNewFile();
}
InputStream in = new FileInputStream(file);
System.out.println(in);
in.close();

 2)、读入数据

方法名说明
public int read()将指定文件读取进内存 一次读取一个字节数据
public int read(byte b[])一次读取一个字节数组数据
public int read(byte b[], int off, int len)一次读取一个字节数组的部分数据,从off位置开始,读取len个长度
// 一次读取一个字节数组
byte[] bs = new byte[1024];  
int len = in.read(bs);
// 如果文件中数据已经读取完毕,没有数据时,read方法会返回一个-1 代表数据读取完毕
while(len != -1){
    System.out.print(new String(bs, 0, len));
    len = in.read(bs);
}
in.close();
    /**
     * 复制mp3文件
     */
    public static void main(String[] args)  {

        //1. 字节输入流, 字节输出流
        File f = new File("D://test.mp3");
        if(!f.exists()){
            System.out.println("文件不存在,请检查路径~~");
        }else{
            InputStream ins = null;
            OutputStream ops = null;
            try {
                //准备好两个流对象
                ins = new FileInputStream(f);
                ops = new FileOutputStream("D://"+UUID.randomUUID()+".mp3");
                //读数据 写数据
                byte[]nums = new byte[1024];
                int len = -1;
                while((len = ins.read(nums))!=-1){
                    //写入数据到新文件
                    ops.write(nums,0,len);
                }
                System.out.println("复制成功!");

                //检查新文件是否成功创建
                System.out.println(new File("D://copy_"+f.getName()).exists());

            } catch (IOException e) {
                System.err.println("出错了!");
            }finally {
                try {
                    if(ins != null || ops != null){
                        ins.close();
                        ops.close();
                    }
                } catch (IOException e) {
                        throw new RuntimeException(e);
                }

            }


        }
    }

3、复制案例(边读入边写出):

public class CopyTest {
    public static void main(String[] args) throws IOException {
        //字节流 复制文件
        InputStream ins  = null;
        OutputStream os  = null;

        try {
            //前期检查
            File file = new File("E://test.mp4");
            if(!file.exists()) file.createNewFile();

            ins = new FileInputStream(file);
            //相对路径
            os = new FileOutputStream("images//mp4_copy.mp4");

            //复制操作   边读边写
            byte[] bs = new byte[1024];
            int len = -1;
            while((len = ins.read(bs))!=-1) {
                //写入
                os.write(bs);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭流
            if(ins != null) ins.close();
            if(os != null) os.close();
        }
    }
}

本电子书目录:《Java基础的重点知识点全集》

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zwarwolf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值