《从零开始的Java世界》10File类与IO流

发布于:2024-04-25 ⋅ 阅读:(24) ⋅ 点赞:(0)

《从零开始的Java世界》系列主要讲解Javase部分,从最简单的程序设计到面向对象编程,再到异常处理、常用API的使用,最后到注解、反射,涵盖Java基础所需的所有知识点。学习者应该从学会如何使用,到知道其实现原理全方位式地学习,才能为以后框架的学习打下良好的基础。

目录

1.File类

1.1概述

1.2实例化

1.2.1构造器

1.3常用方法

2.IO流

2.1概述

2.2分类

2.3流的API

3.文件流(节点流)

3.1FileReader

3.2FileWriter

3.3FileInputStream及FileOutputStream

4.处理流

4.1缓冲流

4.1.1实现步骤

4.1.2实现代码

4.2转换流

4.2.1介绍

4.2.2实现

4.3对象流

4.3.1说明

4.3.2对象的序列化机制

4.3.3使用

序列化过程

反序列化过程

​编辑


1.File类

1.1概述

1.2实例化

1.2.1构造器

1.3常用方法

 

2.IO流

2.1概述

2.2分类

2.3流的API

3.文件流(节点流)

3.1FileReader

读写文本文件用字符流

@Test
    public void test(){
        FileReader fr = null;
        try {
            //1.创建File类对象,对应一个文件
            File file = new File("hello.txt");
            //2.创建输入型的字符流,用于读取数据
            fr = new FileReader(file);
            //3.读取数据,显示在控制台上
            //方式一:
//            int data = fr.read();
//            while (data != -1){
//                System.out.print((char)data);
//                data = fr.read();
            //方式二:
                char buffer[] = new char[5];
                int len = fr.read(buffer);
                while (len != -1){
                    for(int i = 0; i < len; i++){
                        System.out.print(buffer[i]);
                    }
                    len = fr.read(buffer);
                }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流资源
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

3.2FileWriter

@Test
public void test2() {
    FileWriter fw = null;
    try {
        //1.创建File对象,指明操作的文件名称
        File file = new File("info.txt");
        //2.创建输出流
        fw = new FileWriter(file);
        //3.输出过程
        fw.write("ykx666");
    } catch (IOException e) {
        e.printStackTrace();
    }
    //4.关闭资源
    try {
        fw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

3.3FileInputStream及FileOutputStream

读写非文本文件(如图片、音视频)用字节流

@Test
public void test(){
    
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        //1.创建相关的File类对象
        File file1 = new File("sdz.jpg");
        File file2 = new File("copy_sdz.jpg");
        //2.创建相关字节流
        fis = new FileInputStream(file1);
        fos = new FileOutputStream(file2);
        //3.数据的输入和输出
        byte[] buffer = new byte[1024];
        int len;
        while((len = fis.read(buffer)) != -1)
            fos.write(buffer,0,len);
        System.out.println("复制成功!");
    }catch (IOException e) {
        e.printStackTrace();
    }
    //4.关闭资源
    try {
        fis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

4.处理流

4.1缓冲流

4.1.1实现步骤

4.1.2实现代码

/*
 * 使用BufferedInputStream/BufferedOutputStream复制一张图片
 * @author yangkx
 * @date 2023-05-22 19:48
 */
@Test
public void test(){
    FileInputStream fis = null;
    FileOutputStream fos = null;
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
        //1.创建相关的File类对象
        File file1 = new File("sdz.jpg");
        File file2 = new File("copy_sdz2.jpg");
        //2.创建相关字节流、缓冲流
        fis = new FileInputStream(file1);
        fos = new FileOutputStream(file2);

        bis = new BufferedInputStream(fis);
        bos = new BufferedOutputStream(fos);

        //3.数据的输入和输出
        byte[] buffer = new byte[1024];
        int len;
        while((len = bis.read(buffer)) != -1)
            bos.write(buffer,0,len);
        System.out.println("复制成功!");
    }catch (IOException e) {
        e.printStackTrace();
    }
    //4.关闭资源
    //先关外层
    try {
        bis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

4.2转换流

4.2.1介绍

4.2.2实现

4.3对象流

4.3.1说明

4.3.2对象的序列化机制

4.3.3使用

序列化过程

反序列化过程

内容来源于尚硅谷javase课程的ppt,仅作为学习笔记参考