Java重修笔记 第五十九天 坦克大战(九)IO 流 - FileInputStream 和 FileOutputStream

  • IO 流的四个抽象基类

        IO 流的所有类都是这四个基类的派生

(抽象基类)字节流字符流
输入流InputStreamReader
输出流OutputStreamWriter

  • FileInputStream 类常用方法
1. 读取一个字节

        public int read() throws IOException

        返回值:返回读取的到字节,若已经读到文件末尾,则返回 -1 

        异常:IOException - 发生 IO 错误时会抛出此异常

        说明:从该输入流中读取一个字节

    @Test
    public void readFile01() {
        // 定义文件所在路径
        String filePath = "d:\\hello.txt";
        // 定义用来接收读取到的变量
        int readData = 0;
        // 定义文件输入流对象
        FileInputStream fileInputStream = null;

        try {
            // 创建文件输入流对象
            fileInputStream = new FileInputStream(filePath);
            // 循环读取文件内容
            while ((readData = fileInputStream.read()) != -1) {
                // 输出读取的内容
                System.out.print((char) readData);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流, 释放资源
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
2. 读取一串字节

        public int read(byte[] b) throws IOException

        参数:b - 用来接收读取到的字节内容

        返回值:返回实际读取的到字节数,如果读取完毕,则返回 -1 

        异常:IOException - 发生 IO 错误时会抛出此异常

        说明:从该输入流中读取最多b.length字节的数据到 b 数组

    @Test
    public void readFile02() {
        // 定义文件所在路径
        String filePath = "d:\\hello.txt";
        // 定义用来接收读取到的变量
        int readLength = 0;
        // 定义字节数组用来接收读取到的字节
        byte[] buf = new byte[8];

        // 定义文件输入流对象
        FileInputStream fileInputStream = null;

        try {
            // 创建文件输入流对象
            fileInputStream = new FileInputStream(filePath);
            // 循环读取文件内容
            while ((readLength = fileInputStream.read(buf)) != -1) {
                // 将字符数组转成字符串输出读取的内容
                    System.out.print(new String(buf,0,readLength));
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流, 释放资源
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
3. 关闭文件流

        public void close() throws IOException

        异常:IOException - 发生 IO 错误时会抛出此异常

        说明:关闭该输入流,释放资源

  •  FileOutputStream 类常用方法
1. 构造器并指定写入方式

        public FileOutputStream(String name, boolean append) throws FileNotFoundException

        参数:name - 文件路径        append - 写入方式

        异常:FileNotFoundException - 如果文件不存在但不能创建,或存在但是是一个目录而不是常规文件,或由于任何其他原因无法打开时会抛出此异常

        说明:append 为 true 时,文件写入是以追加的方式写入,append 为 false 时,文件写入是以覆盖的方式写入(不写默认覆盖),且如果指定的文件路径不存在时会创建该文件

2. 写入一个字节

        public void write(int b) throws IOException

        参数:b - 要写入的字节

        异常:IOException - 发生 IO 错误时会抛出此异常

        说明:向目标文件以构造器制定的形式写入一个字节

3. 写入一串字节

        public void write(byte[] b) throws IOException

        参数:b - 要写入的目标字节数组

        异常:IOException - 发生 IO 错误时会抛出此异常

        说明:向目标文件以构造器制定的形式写入 b 中的数据

4. 写入一串指定的字节

        public void write(byte[] b, int off, int len) throws IOException

        参数:b - 要写入的目标字节数组        off - 数据中的起始下标偏移量       len - 要写入的长度 

        异常:IOException - 发生 IO 错误时会抛出此异常

        说明:向目标文件以构造器制定的形式写入 b 中的数据,从 off 下标开始写入 len 长度的数据

5. 关闭文件流

        public void close() throws IOException

        异常:IOException - 发生 IO 错误时会抛出此异常

        说明:关闭该输出流,释放资源

    @Test
    public void writeFile01() {
        // 定义文件路径
        String filePath = "d:\\a.txt";
        // 定义 FileOutputStream 文件字节输出流对象
        FileOutputStream fileOutputStream = null;
        try {
            // 创建文件字节流输出对象并指定以追加的方式写入
            fileOutputStream = new FileOutputStream(filePath, true);
            // 写入一个字节
            // fileOutputStream.write('a');
            // 写入字符串, 从下标0开始写入5个进去
            fileOutputStream.write("Hello World!".getBytes(StandardCharsets.UTF_8), 0, 5);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭字节流, 节约资源
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

  • 文件拷贝
public class FileCopy01 {
    public static void main(String[] args) {
        // 拷贝文件
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        String filePath = "d:\\hnt.png";
        String destPath = "d:\\hntCopy.png";
        byte[] buf = new byte[1024];
        int read = 0;
        try {
            fileInputStream = new FileInputStream(filePath);
            fileOutputStream = new FileOutputStream(destPath, true);
//            while ((read = fileInputStream.read()) != -1) {
//                fileOutputStream.write(read);
//            }
            while ((read = fileInputStream.read(buf)) != -1) {
                fileOutputStream.write(buf, 0, read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值