Java入门学习第十七天——字节流读写文件、字节缓冲流拷贝文件

49 篇文章 0 订阅
21 篇文章 0 订阅

字节流读写文件

字节流拷贝文件—按单个字节读写

FileInputStream:普通的字节输入流,用来读取数据的

构造方法:

​ public FileInputStream(String pathname);

成员方法:

​ public int read();

单个:一次读取一个字节,并返回读取到的内容,读不到返回-1

数组:一次读取一个字节数组,将读取到的内容存入到数组中,并返回读取到的有效字节数,读不到则返回-1

FileOutputStream:普通的字节输出流,用来写数据

构造方法:

​ public FileOutputStream(String pathname);

成员方法:

​ public void write(int len);

单个:一次写入一个字节

数组:一次写入一个指定的字节数组

步骤:

创建字节流读文件对象:

InputStream is = new FileInputStream(“1.jpg”);

创建字节流写文件对象:

OutputStream os = new FileOutputStream(“D:\2.jpg”);

使用while循环读写数据:

int b;

while (b = is.read() != -1) {

​ os.write(b);

}

异常处理:

throws IOException

关闭资源:

is.close();

os.close();

自行在相关文件夹下放入图片

package io.demo;

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

public class StreamDemo {
    public static void main(String[] args) throws IOException {
//        需求:通过普通的字节流,一次读写一个字节的方式,将a.jpg复制到b.jpg中
//        1.创建字节输入流,关联数据源文件
        FileInputStream is = new FileInputStream("lib/1.jpg");
//        2.创建字节输出流,关联目的地文件
        FileOutputStream os = new FileOutputStream("lib/2.jpg");
//        3.定义变量,记录读取到的内容
        int i;
//        4.循环读取,只要条件满足就一直读取,并将读取到的内容渎职给变量
        while ((i = is.read()) != -1) {
//        5.将读取到的内容写入目的地文件中
            os.write(i);
        }
//        6.释放资源
        is.close();
        os.close();
    }
}
字节流拷贝文件—按字节数组读写

创建字节流读文件对象:

InputStream is = new FileInputStream(“1.jpg”);

创建字节流写文件对象:

OutputStream os = new FileOutputStream(“D:\2.jpg”);

异常处理:

throws IOException

关闭资源:

is.close();

os.close();

定义字节数组,每次读取2048个字节:

byte[] b = new byte[2048];

使用while循环读写数据:

int i;

while (i = is.read() != -1) {

​ os.write(b,0,i);

}

package io.demo;

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

public class StreamDemo2 {
    public static void main(String[] args) throws IOException {
//        需求:通过普通的字节流,一次读写一个字节数组的方式,将a.jpg复制到b.jpg中
//        1.创建字节输入流对象,关联数据源文件
        FileInputStream is = new FileInputStream("lib/1.jpg");
//        2.创建字节输出流对象,关联目的地文件
        FileOutputStream os = new FileOutputStream("lib/3.jpg");
//        3.定义变量,接收读取到的内容
        byte[] b = new byte[1024];              //[]里面写多少都行,最好是写1024的整数倍
//        用来记录读取到的有效字节数
        int len;
//        4.循环读取,只要条件满足,就一直读取,并将读取到的内容(有效的字节数)赋值给变量
        while ((len = is.read(b)) != -1) {
//        5.将读取到的数据写入目的地文件
            os.write(b,0,len);
        }
//        6.释放文件
        is.close();
        os.close();
    }
}
字节缓冲流拷贝文件的标准代码

BufferedInputStream:字节缓冲输入流(也叫高效字节输入流),用来读数据的;

构造方法:

​ public BufferedInputStream(InputStream is);

成员方法:

​ public int read();

单个:一次读取一个一个字节,并返回读取到的内容,读不到返回-1;

BufferedOutputStream:字节缓冲输出流(也叫高效字节输出流),用来写数据的;

构造方法:

​ public BufferedOutputStream(OutputStream os);

成员方法:

​ public void write(int len);

单个:一次写入一个字节;

**特点:**字节缓冲流有自己的缓冲区,大小为8192个字节,也就是8KB

步骤:

创建字节缓冲流读文件对象:

​ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(“1.jpg”));

创建字节缓冲流写文件对象:

​ BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(“2.jpg”));

异常处理:

throws IOException

关闭资源:

is.close();

os.close();

使用while循环读写数据:

int i;

while (i = bis.read() != -1) {

​ os.write(i);

}

package io.demo;

import java.io.*;
//总结:
//        拷贝纯文本文件使用字符流,拷贝其他(图片,视频,音频)使用字节流
public class StreamDemo3 {
    public static void main(String[] args) throws IOException {
//需求:通过字节缓冲流,将1.jpg复制到2.jpg中
//        1.创建字节输入流对象,关联数据源文件
//        分写
//        FileInputStream is = new FileInputStream("lib/1.jpg");
//        BufferedInputStream bis = new BufferedInputStream(is);
//        合并
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("lib/1.txt"));
//        2.创建字节输出流对象,关联目的地文件
//        分写
//        FileOutputStream os = new FileOutputStream("lib/2.jpg");
//        BufferedOutputStream bos = new BufferedOutputStream(os);
//        合并
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("lib/2.txt"));
//        3.定义变量,用来存放读取到的数据
        int len;
//        4.循环读取,只要条件满足就一直读取,并将读取到的内容赋值给变量
        while ((len = bis.read()) != -1) {      //底层是按照字节数组来操作的
            //        5.将读取到的内容写入目的地文件中
            bos.write(len);
        }
//        6.释放资源
        bis.close();
        bos.close();
    }
}

**总结:**拷贝纯文本文件使用字符流,拷贝其他(图片,视频,音频)使用字节流。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值