10.File/IO流-bite

文件对象

目录又称为文件夹,文件夹也是一个文件,具体的目录和文件都可以用File对象来表示 .\表示该层所在的文件夹
在这里插入图片描述

在这里插入图片描述

字节流对象和字符流对象

字节流对象:
读:InputStream(抽象类)->FileInputStream(实现子类)
写:OutputStream(抽象类)->FileOutputStream(实现子类)

FileInputStream:
在这里插入图片描述
read提供了三个版本的重载.
1)无参数版本:一次读一个字节.返回值是读到的这个字节:返回值是对应字节的ASCII码值,读完了返回-1

2)一个参数版本:一次读若干个字节,把读的结果放到参数中指定的数组中,返回值就是读到的字节数.
返回填充的字节数组长度,读完返回-1

3)三个参数版本:一次读若干个字节,把读的结果放到参数中指定的数组中,返回值就是读到的字节数.不是从数组的起始位置放置,而是从中间位置放置(off这个下标的位置)len表示最多能放多少个元素(字节)

        // 构造方法中需要指定打开文件的路径.
        // 此处的路径可以是绝对路径, 也可以是相对路径, 还可以是 File 对象
        InputStream inputStream = null;
        try {
            // 1. 创建对象, 同时也是在打开文件.
            inputStream = new FileInputStream("d:/test.txt");
            // 2. 尝试一个一个字节的读, 把整个文件都读完.
            while (true) {
                int b = inputStream.read();
                if (b == -1) {
                    // 读到了文件末尾
                    break;
                }
                System.out.println(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 3. 读完之后要记得关闭文件, 释放资源~
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

另一种写法:由try自动调用close

        try (InputStream inputStream = new FileInputStream("d:/test.txt")) {
            while (true) {
                int b = inputStream.read();
                if (b == -1) {
                    break;
                }
                System.out.println(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

一次读若干个字节放在数组中:

try (InputStream inputStream = new FileInputStream("d:/test.txt")) {
            // 一次读取若干个字节.
            while (true) {
                byte[] buffer = new byte[1024];
                int len = inputStream.read(buffer);
                if (len == -1) {
                    // 如果返回 -1 说明读取完毕了
                    break;
                }
                for (int i = 0; i < len; i++) {
                    System.out.println(buffer[i]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

FileOutputStream:每次写入文件是会清空原有文件内容,可以在构造方法后面再加上一个参数:true,使之不清空,接着原文件后面写入

// 使用字节流, 写文件的案例.
public class Demo9 {
    public static void main(String[] args) {
        try (OutputStream outputStream = new FileOutputStream("d:/test.txt")) {
//            outputStream.write(97);
//            outputStream.write(98);
//            outputStream.write(99);

            byte[] buffer = new byte[]{97, 98, 99};
            outputStream.write(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

字符流对象:
读:Reader->FileReader

// 按照字符来读写
public class Demo10 {
    public static void main(String[] args) {
        try (Reader reader = new FileReader("d:/test.txt")) {
            // 按照字符来读.
            while (true) {
                char[] buffer = new char[1024];
                int len = reader.read(buffer);
                if (len == -1) {
                    break;
                }
//                for (int i = 0; i < len; i++) {
//                    System.out.println(buffer[i]);
//                }
                // 如果这里传入的 数组 是 byte 数组, 还可以手动的指定一下 utf8 字符集避免乱码.
                String s = new String(buffer, 0, len);
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

写:Writer->FileWriter

// 按照字符来写
public class Demo11 {
    public static void main(String[] args) {
        try (Writer writer = new FileWriter("d:/test.txt")) {
            writer.write("xyz");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值