IO流学习笔记

IO流

Java程序中对于数据的输入输出都是以流的形式进行。

按操作数据单位不同分为:字节流,字符流

按数据流的流向不同分为:输入流,输出流

按流的角色的不同分为:节点流,处理流

Java 的 IO 流共涉及 40 多个类,实际上非常规则,都 是从如下 4 个抽象基类派生的。 由这四个类派生出来的子类名称都是以其父类名作为 子类名后缀。

image-20220808210956526

image-20220808211045043

1.1 访问文件的字节输入流(FileInputStream)

FileInputStream不具有缓冲的效果,一般要与BufferedInputStream连用

//字节输入流
public class Demo1 {
    public static void main(String[] args) throws Exception {
        // 创建一个File类对象
        File file = new File("d:/a.txt");
        // 把File类对象传到文件输入流中 FileInputStream一次只读取一个字节
        FileInputStream fis = new FileInputStream(file);
        // 把 fis 传到缓冲输入流中,让其具有缓冲功能
        BufferedInputStream bis = new BufferedInputStream(fis);
        // 定义一个缓冲数组(字节流的一般定义为长度为4096字节,这个长度就是每次传入到缓冲区的数据长度)
        // 这个数组就是缓冲区
        byte[] bytes = new byte[8];
        int length = -1;
        // bis.read(bytes) : 从输入流中读取一些数据,传入到缓冲区bytes中
        //
        while ((length = bis.read(bytes)) != -1) {
            System.out.println(bis.read(bytes));
            System.out.println(length);
            System.out.println(new String(bytes,0,length));
        }
        // io流用完之后一定要关闭
        //先开的后关,后开的先关
        fis.close();
        bis.close();
    }
}

1.2 访问文件的字节输出流(FileOutputStream)

//字节输出流 :把内存中的字符串写到磁盘的对应文件中
public class Demo2 {
    public static void main(String[] args) throws IOException {
        //创建一个file对象,指向磁盘的对应文件
        File file = new File("d:/a.txt");
        // 把File类对象传到文件输出流中 FileOutputStream一次只读取一个字节
        FileOutputStream fos = new FileOutputStream(file);
        // 把 fos 传到缓冲输入流中,让其具有缓冲功能
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        // 把字符串转为字节数组
        byte[] bytes = new String("lalala").getBytes();
        // 写到磁盘中
        bos.write(bytes);
        // 关闭流
        bos.close();
        fos.close();
    }
}

1.3访问文件的字符输入流(FileReader)

一般不会使用字符流操作图片 音频 视频等,因为牵涉到解码。会出问题!!!

开发一般使用字节流!!!

// 字符输入流
public class Demo3 {
    public static void main(String[] args) throws IOException {
        // 新建一个对象,具有缓冲功能的字符输入流对象
        BufferedReader br = new BufferedReader(new FileReader(new File("d:/ww/test/1.txt")));
        // 定义缓冲字符数组,这个数组其实就是缓冲区
        char[] chars = new char[4 * 1024];
        ///如果chars的长度为零,则不会读取字符并返回0 ;
        //  否则,尝试读取至少一个字节。
        // 如果没有字节可用,因为流在文件末尾,则返回值-1
        int length = -1;
        while ((length = br.read(chars)) != -1) {
            // 打印到控制台
            System.out.println(new String(chars,0,length));
        }
    }
}

1.4 访问文件的字符输出流(FileWriter)

// 字符输出流
public class Demo4 {
    public static void main(String[] args) throws IOException {
        // 新建一个对象,具有缓冲功能的字符输出流对象
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File("d:/ww/test/1.txt")));
        //把内存中的字符串写入到磁盘中
        bw.write("哈哈哈哈哈");

    }
}

1.5 输入输出流的连用

public class Demo1 {
    public static void main(String[] args) throws IOException {
        // 创建一个带有缓冲功能的字节输入流对象
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("d:/ww/test/1.jpg")));
        // 创建一个带有缓冲功能的字节输出流对象
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("d:/ww/test1/1.jpg")));
        // 定义缓冲数组,长度为缓冲区的大小
        byte[] bytes = new byte[4 * 1024];
        int length = -1;
        // bis.read(bytes) 数据写入了bytes中,读过之后流就没有了,把字节长度赋值给length
        while ((length = bis.read(bytes)) != -1) {
            // 把读入到bytes中的数据通过write写到磁盘中,偏移量为0,每次写入的长度为length
            bos.write(bytes,0,length);
        }
        // 关闭IO流
        bos.close();
        bis.close();
    }
}
posted @   逗逗(小白)  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示