I/O流综合简介——字节流(二)

本文介绍了Java中的字节流,包括字节输出流OutputStream、字节输入流InputStream的使用,异常处理策略,以及字节流与缓冲区结合的应用,通过实例展示了不同读写方式对文件复制效率的影响。

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

小梁同学 の

Java学习旅途

你好! 这是小梁同学使用 博客 所记录的文章笔记,作为一个初学者的从基础到未来的记录,如果你想和我一起在Java学习路程上坚持下去,欢迎你的关注与指正。

新的将来

万丈高楼平地起,未来只能靠自己
从无到有,从零到一,学习路上没有尽头
每日一文,每日一记,跟着软件向前努力
加油!!!!!

字节流

一.字节输出流(字节流写数据) OutputStream

直接创建的话会显示OutputStream抽象类不能够实例化对象,故选择它的子类FileOutputStream创建字节输出流对象,代码如下:

在这里插入图片描述

import java.io.FileOutputStream;
import java.io.IOException;

public class Test1 {
    public static void main(String[] args) throws IOException {
        //抽象类不能够实例化对象
//      OutputStreamtream outputStream = new OutputStream();

//      1.创建字节输出流对象
//(1)覆盖写,每次重新运行程序都会在创建的文本中重新写入数据
        FileOutputStream fops = new FileOutputStream("./myBook.txt");  
//(2)追加写,通过构造方法中的第二个参数来决定
		//true,则在原文本后面加上新写入的
		//false,则表示不追加写,与覆盖写异曲同工
		FileOutputStream fops = new FileOutputStream("./myBook.txt"true); 

        //2.数据的传输操作
//(1)正常一个一个写入
        fops.write(68);//'D'
		fops.write(69);//'E'
//(2)向文件中写入26个英文字母
        for (int i = 0; i < 26; i++) {
            fops.write(65 + i);
        }
//(3)写一个byte数组
		byte[] bytes = {97,98,99,100,101,102};
		fops.write(bytes);//'abcdef'
//(4)将一个字符串转换未byte类型数组,getBytes()
		byte[] bytes = "187288837298你好,小白".getBytes();
		//第一个参数为起始点,后一个为截止长度
        fops.write(bytes,0,16);

        //3.关闭流,释放资源
        fops.close();
    }

}

二.字节输入流(字节流读数据) InputStream

与字节输出流一样,直接创建的话会显示InputStream抽象类不能够实例化对象,故选择它的子类FileInputStream创建字节输出流对象,代码如下:

import java.io.FileInputStream;
import java.io.IOException;

public class Test2 {
    public static void main(String[] args) throws IOException {
//1.创建字节输入流对象
        FileInputStream fips = new FileInputStream("./myBook.txt");
//2.操作输入流数据
        //(1)一次读一个字节
//        int by = fips.read();
//        by = fips.read();
//        by = fips.read();
//        System.out.println(by);//当没有数字时,by会输出-1.

        //(2)单个字节循环读取
//        int by;
//        while ((by = fips.read())!=-1){
//        System.out.println((char)by);
//        }

        //(3)一次读取一个数组
        byte[] bytes = new byte[10];//用来读取数据的缓冲区
        int len = fips.read(bytes);//返回的是数组中实际数据的长度
        System.out.println(new String(bytes));
        System.out.println(new String(bytes,0,len));

        //(4)通过数组循环的方式读取数据
        //将数据读取到byte数组中时,如果有数据则返回实际数据的长度,如果没有数据,则返回-1
        while ((len=fips.read(bytes))!=-1) {
            System.out.println(new String(bytes));
            System.out.println(new String(bytes, 0, len));
        }

//3.关闭连接并释放资源
        fips.close();
    }
}

三.字节流异常处理

字节流中异常处理可以通过抛出或捕获的方式进行处理,但不推荐捕获,因为相对于抛出而言,捕获的代码不易规整,以下代码为捕获处理异常,仅供参考:

import java.io.FileOutputStream;
import java.io.IOException;

public class Test1 {
    public static void main(String[] args)  {
        //1、创建字节输出流对象
        FileOutputStream fops = null; //覆盖写
        try {
            fops = new FileOutputStream("./myBook.txt");
//          2、数据的传输操作
            byte[] bytes = "1872367882你好小白".getBytes();//将字符串转换成byte类型数组
            fops.write(bytes,0,22);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
//        3、关闭流,释放资源
            try {
                fops.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

四.字节流——读一个字节,写一个字节

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class test3 {
    public static void main(String[] args) throws IOException {
        //创建字节输入流对象
        FileInputStream fips = new FileInputStream("./myTest.txt");
        //创建字节输出流对象
        FileOutputStream fops = new FileOutputStream("./newMyTest.txt");
        int by;
        while ((by = fips.read())!=-1){//读一个字节同时写一个字节
            fops.write(by);
        }
        //关闭流,释放资源
        fips.close();
        fops.close();
    }
}

五.字节流缓冲区

分别定义四个方法来进行对比看所复制文件的时间:

基本字节流一次读写一个字节
基本字节流一次读写一个数组
缓冲区字节流一次读写一个字节
缓冲区字节流一次读写一个数组
import java.io.*;
public class Test3 {
    public static void main(String[] args) throws IOException{
        final String VCR = "./video.mp4";
        final String TO = "./newVideo.mp4";

        long startTime = System.currentTimeMillis();//开始时的毫秒数
        method1(VCR,TO);
        //method2(VCR,TO);
        //method3(VCR,TO);
        //method4(VCR,TO);

        long endTime = System.currentTimeMillis();//结束时的毫秒数
        System.out.println("复制该文件共耗时:"+(endTime - startTime)+"毫秒");
    }
    //定义method1方法:基本字节流去一次读写一个字节,实现文件Copy
    private static void method1(String vcr, String to) throws IOException {
        //1.创建字节流对象
        FileInputStream fips = new FileInputStream(vcr);
        FileOutputStream fops = new FileOutputStream(to);
        //2.操作字节流数据
        int by;
        while ((by = fips.read())!=-1){
            fops.write(by);
        }
        //3.关闭连接并释放资源
        fips.close();
        fops.close();
    }

    //定义method2方法:基本字节流去一次读写一个数组,实现文件Copy
    private static void method2(String vcr, String to) throws IOException {
        //1.创建字节流对象
        FileInputStream fips = new FileInputStream(vcr);
        FileOutputStream fops = new FileOutputStream(to);
        //2.操作字节流数据
        byte[] bytes = new byte[1024];
        int len;
        while ((len = fips.read(bytes))!=-1){
            fops.write(bytes,0,len);
        }
        //3.关闭连接并释放资源
        fips.close();
        fops.close();
    }

    //定义method3方法:缓冲区字节流去一次读写一个字节,实现文件Copy
    private static void method3(String vcr, String to) throws IOException {
        //1.创建字节流对象
        BufferedInputStream bips = new BufferedInputStream(new FileInputStream(vcr));
        BufferedOutputStream bops = new BufferedOutputStream(new FileOutputStream(to));
        //2.操作字节流数据
        int by;
        while ((by = bips.read())!=-1){
            bops.write(by);
        }
        //3.关闭连接并释放资源
        bips.close();
        bops.close();
    }

    //定义method4方法:缓冲区字节流去一次读写一个数组,实现文件Copy
    private static void method4(String vcr, String to) throws IOException {
        //1.创建字节流对象
        BufferedInputStream bips = new BufferedInputStream(new FileInputStream(vcr));
        BufferedOutputStream bops = new BufferedOutputStream(new FileOutputStream(to));
        //2.操作字节流数据
        byte[] bytes = new byte[1024];
        int len;
        while ((len = bips.read(bytes))!=-1){
            bops.write(bytes,0,len);
        }
        //3.关闭连接并释放资源
        bips.close();
        bops.close();
    }


}

以上内容是我在学习过程中所熟悉了解了的,希望对广大网友有所帮助,如果有错误,欢迎批评指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值