【Java】IO流 - 字节流

➤ Java 输入输出IO流 全部导航

在这里插入图片描述

在这里插入图片描述

FileInputStream 介绍

在这里插入图片描述
在这里插入图片描述

  • 创建一个txt文件,写入 HelloWorld 并用Java读取:
@Test
public void readFile01(){
    //提前创建一个文件hello.txt并编辑一个HelloWorld
    String filePath = "d:\\hello.txt";
    int readData = 0;
    FileInputStream fileInputStream = null;//定义在外面,扩大作用域
    try {  //创建FileInputStream对象,用于读取文件
        fileInputStream = new FileInputStream(filePath);
        //从该输入流读取一个字节的数据,如果没有输入可用,此方法则阻止
        //如果返回-1,表示读取完毕
        while((readData = fileInputStream.read()) != -1){
            System.out.print((char)readData);//把数字转为字符
        }
    } catch (IOException e) {
    }finally{
        //关闭文件流,释放资源
        try {
            fileInputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

因为read()方法是一个字节一个字节读取,英文和数字占一个字节,而汉字占三个字节(UTF-8编码下),所以read方法读取汉字会出现乱码,要使用 read(byte[] b)参数方法来几个字节几个字节的读取:

使用重载方法:read(byte[] b) 几个几个字节读取:

@Test
public void readFile02(){
    //提前创建一个文件hello.txt并编辑一个HelloWorld你好
    String filePath = "d:\\hello.txt";
    byte[] buf = new byte[8];//一次读取8个字节
    FileInputStream fileInputStream = null;//定义在外面,扩大作用域
    try {
        //创建FileInputStream对象,用于读取文件
        fileInputStream = new FileInputStream(filePath);
        //从该输入流读取最多b.length字节的数据到字节数组。
        //如果返回-1,表示读取完毕
        //如果读取正常,返回实际读取的字节数(注意是实际)
        while((fileInputStream.read(buf)) != -1){
            System.out.print(new String(buf,0, buf.length));//把数字转为字符
        }
    } catch (IOException e) {
    }finally{
        //关闭文件流,释放资源
        try {
            fileInputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

FileOutputStream介绍

在这里插入图片描述
// new FileOutputStream(filePath)创建方式:写入内容会覆盖原来的内容
// new FileOutputStream(filePath,true)创建方式:写入内容是追加内容,不覆盖

在这里插入图片描述
在这里插入图片描述

/*
演示使用 FileOutputStream 将数据写入到文件中
如果文件存在,则创建该文件
 */
@Test
public void writeFile(){
    //创建FileOutputStream对象
    String filePath = "d:\\a.txt";
    FileOutputStream fileOutputStream = null;//扩大作用域
    try {
        //得到FileOutputSteam对象
        // new FileOutputStream(filePath)创建方式:写入内容会覆盖原来的内容
        // new FileOutputStream(filePath,true)创建方式:写入内容是追加内容,不覆盖
        fileOutputStream = new FileOutputStream(filePath);
        //写入一个字节
        fileOutputStream.write('a');
        //写入字符串
        String str = " Hello World ";
        //str.getBytes() 可以把 字符串->字节数组
        fileOutputStream.write(str.getBytes());
        /*
        write(byte[] b,int off,int len)
        将len字节从位于偏移量off的指定字节数组写入此文件输出流
         */
        fileOutputStream.write(str.getBytes(),0,str.length());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

文件输入输出综合使用【拷贝】

在这里插入图片描述

读取一部分就输出一部分,边读边输出,这样可以避免文件过大读取太久
(使用循环解决,分段输入输出)

public static void main(String[] args) {
    //文件拷贝:将 d:\\蜘蛛侠.jpg 拷贝到 d:\\spiderman.jpg
    //1.创建文件的输入流,将文件读取到程序
    //2.创建文件的输出流,将读取到的文件数据,写入到指定的文件
    String srcFilePath = "d:\\蜘蛛侠.jpg";
    String desFilePath = "d:\\spiderman.jpg";
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    try {
        fileInputStream = new FileInputStream(srcFilePath);
        fileOutputStream = new FileOutputStream(desFilePath);
        //定义一个字节数组,提高读取效果
        byte[] buf = new byte[1024];
        int readLen=0;
        while((readLen = fileInputStream.read(buf)) != -1){
            //读取到后,立马通过FileOutputStream写入文件(为了提高速度)
            //即边读边写
            fileOutputStream.write(buf,0,readLen);//一定要用这个方法
        }
        System.out.println("拷贝完成~");
    } catch (IOException e) {
        System.out.println(e);
    } finally {
        try {
            //关闭输入流和输出流,释放资源
            if(fileInputStream != null) {
                fileInputStream.close();
            }
            if(fileOutputStream != null) {
                fileOutputStream.close();
            }
        } catch (IOException x) {
            throw new RuntimeException(x);
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

苗半里

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

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

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

打赏作者

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

抵扣说明:

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

余额充值