Java笔记 - IO流

一、File文件类

1、文件基本操作

public class FileDemo {
    public static void main(String[] args) throws IOException {

        File file1 = new File("E:\\code\\maven_code\\reflection-stu\\src\\main\\resources\\file001.txt");
        File file2 = new File(new File("E:\\code\\maven_code\\reflection-stu\\src\\main\\resources"), "file002");
        File file3 = new File("E:\\code\\maven_code\\reflection-stu\\src\\main\\resources", "file003.txt");

        // 创建文件
        file1.createNewFile();
        file2.createNewFile();
        file3.createNewFile();

        // 文件名
        System.out.println(file1.getName());
        // 文件绝对路径
        System.out.println(file1.getAbsoluteFile());
        // 文件目录
        System.out.println(file1.getParent());
        // 文件大小
        System.out.println(file1.length());
        // 文件是否存在
        System.out.println(file1.exists());
        // 文件是否为文件
        System.out.println(file1.isFile());
        // 文件是否为目录
        System.out.println(file1.isDirectory());
        // 文件删除
        file1.delete();
    }
}

二、IO流的分类

        (1)按操作数据单位不同分为:字节流(8 bit)二进制文件,字符流(按字符)文本文件。

        (2)按数据流的流向不同分为:输入流,输出流。

        (3)按流的角色的不同分为:节点流,处理流、包装流。

三、输入流和输出流

1、FileInputStream

        文件输入字节流

public class FileInputStreamDemo {
    public static void main(String[] args) throws IOException {

        String filePath = "E:\\code\\maven_code\\reflection-stu\\src\\main\\resources\\file003.txt";

        FileInputStream input = new FileInputStream(filePath);

        // 方法一、一个字节一个字节读
        int item = 0;
        while ((item = input.read()) != -1) {
            System.out.print((char) item);
        }
        
        // 方法二、一组字节一组字节读
        byte[] items = new byte[8];
        int readline = 0;
        while ((readline = input.read(items)) != -1) {
            System.out.print(new String(items, 0, readline));
        }
        
        input.close();
    }
}

2、FileOutputStream

        文件输出字节流

public class FileOutputStreamDemo {
    public static void main(String[] args) throws IOException {

        String filePath = "E:\\code\\maven_code\\reflection-stu\\src\\main\\resources\\file004.txt";
        String str = "helloworld";
        byte[] a;
        // true 表示追加,flase表示覆盖
        FileOutputStream output = new FileOutputStream(filePath,true);

        output.write(str.getBytes());
        output.write(a,0,a.length()))

        output.close();
    }
}

3、FileReader

        文件输入字符流

public class FileReaderDemo {
    public static void main(String[] args) throws IOException {
        String filePath = "E:\\code\\maven_code\\reflection-stu\\src\\main\\resources\\file003.txt";

        FileReader reader = new FileReader(filePath);

        int item = 0;
        while ((item = reader.read()) != -1) {
            System.out.print((char) item);
        }

        char[] items = new char[8];
        int readline = 0;
        while ((readline = reader.read(items)) != -1) {
            System.out.print(new String(items, 0, readline));
        }

        reader.close();
    }
}

4、FileWriter

        文件输出字符流

public class FileWriterDemo {
    public static void main(String[] args) throws IOException {
        String filePath = "E:\\code\\maven_code\\reflection-stu\\src\\main\\resources\\file004.txt";
        String str = "helloworld";
        char[] chars = str.toCharArray();

        FileWriter writer = new FileWriter(filePath);

        writer.write(str);
        writer.write(chars, 0, chars.length);
        writer.close();
    }
}

四、节点流和处理流

        (1)节点流

                节点流可以从一个特定的数据读写数据,入FileReader、FileWriter。

                节点流是底层流、低级流,直接跟数据源相接。

        (2)处理流

                处理流(包装流)是连接在已存在的流之上,为程序提供更为强大的读写功能,也更加灵活,入BufferedReader,BufferedWriter。

                处理流(包装流)包装节点流,既可以消除不同节点流的实现差异,也可以提供更方便的方法来完成输入输出。

处理流(包装流)对节点流进行包装,使用了装饰器设计模式,不会直接与数据源相连。

        (3)处理流优点

                ①性能提高,主要以增加缓冲的方式来提高输入输出的效率。

                ②操作便捷,处理流可以提供一系列便捷的方法来一次输入输出大批量的数据,使用更加灵活方便。

五、缓冲流

1、BufferedReader

        字符输入缓冲流

public class BufferedReaderDemo {
    public static void main(String[] args) throws IOException {
        String filePath = "E:\\code\\maven_code\\reflection-stu\\src\\main\\resources\\file003.txt";

        BufferedReader bufReader = new BufferedReader(new FileReader(filePath));

        String line;
        // 按行读取文件,返回为空时读取完毕
        while ((line = bufReader.readLine()) != null) {
            System.out.println(line);
        }
        // 关闭流,只需要关闭包装流,底层对自动关闭节点流
        bufReader.close();
    }
}

2、BufferedWriter

        字符输出缓冲流

public class BufferedWriterDemo {
    public static void main(String[] args) throws IOException {
        String filePath = "E:\\code\\maven_code\\reflection-stu\\src\\main\\resources\\file005.txt";
        String str = "helloworld";

        BufferedWriter bufWriter = new BufferedWriter(new FileWriter(filePath,true));

        bufWriter.write(str);
        bufWriter.newLine(); //换行
       bufWriter.close();
    }
}

3、BufferedInputStream

        字节输入缓冲流

public class BufferedInputStreamDemo {
    public static void main(String[] args) throws IOException {
        String filePath = "E:\\code\\maven_code\\reflection-stu\\src\\main\\resources\\file003.txt";

        BufferedInputStream bufInput = new BufferedInputStream(new FileInputStream(filePath));

        byte[] bytes = new byte[8];
        int flag = 0;

        while ((flag = bufInput.read(bytes)) != -1) {
            System.out.print(new String(bytes, 0, flag));
        }

        bufInput.close();
    }
}

4、BufferedOutputStream

        字节输出缓冲流

public class BufferedOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        String filePath = "E:\\code\\maven_code\\reflection-stu\\src\\main\\resources\\file006.txt";
        String str = "helloworld";

        BufferedOutputStream bufOutput = new BufferedOutputStream(new FileOutputStream(filePath,true));

        bufOutput.write(str.getBytes(StandardCharsets.UTF_8));

        bufOutput.close();
    }
}

六、对象流

1、序列化与反序列化

        ①序列化就是在保存数据时,保存数据的值和数据类型。

        ②反序列化就是在恢复数据时,恢复数据的值和数据类型。

        ③需要让某个对象执行序列化机制,则必须让其类是可序列化的,为了让某个类是可序列化的,该类必须实现如下两个接口之一:

                Serializable // 标记接口,没有方法

                Externalizable // 该接口有方法需要实现

2、ObjectInputStream

        提供反序列功能

public class ObjectInputStreamDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        String filePath = "E:\\code\\maven_code\\reflection-stu\\src\\main\\resources\\obj002.obj";

        ObjectInputStream objInput = new ObjectInputStream(new FileInputStream(filePath));

        Object dog1 = (Dog) objInput.readObject();

        System.out.println(dog1.toString());

        objInput.close();
    }
}

3、ObjectOutputStream

        提供序列化功能

public class ObjectOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        String filePath = "E:\\code\\maven_code\\reflection-stu\\src\\main\\resources\\obj001.obj";
        Dog dog1 = new Dog("aaa", 10, "boy");
        Dog dog2 = new Dog("bbb", 11, "boy");
        Dog dog3 = new Dog("ccc", 12, "boy");

        ObjectOutputStream objOutput = new ObjectOutputStream(new FileOutputStream(filePath));

        objOutput.writeObject(dog1);
        objOutput.writeObject(dog2);
        objOutput.writeObject(dog3);

        objOutput.close();

    }
}

4、注意事项

        ①读写要一致

        ②要求序列化和反序列化对象,需要实现Serializable

        ③序列化的类中建议添加SerialVersionUID,提高版本兼容性。(SerialVersionUID 相同的视为同一对象)

        ④序列化对象时,默认将里面所有属性都进行序列化,但除了static和transient修饰的成员。

        ⑤序列化对象时,要求里面属性的类型也需要实现序列化接口。

        ⑥序列化具备可继承性,也就是如果某类已经实现了序列化,则它的所有子类也已经默认实现了序列化。

七、标准输入、输出流

1、输入流

        System.in 键盘

2、标准输出流

        System.out 显示器

八、转换流

        ①InputStreamReader:Reader的子类,可以将InputStream(字节流)包装成Reader(字符流)。

        ②OotputStreamWriter:Writer的子类,实现将OutputStream(字节流)包装成Writer(字符流)。

        ③当处理纯文本数据时,如果使用字符流效率更高,并且可以优先解决中文问题,所有建议将字节流转换成字符流。

        ④可以在使用时指定编码格式。

1、InputStreamReader

        输入转换流

public class InputStreamReaderDemo {
    public static void main(String[] args) throws IOException {
        /*
        以字节流的形式读取文件,通过转换流转化为utf8编码的字符流,并包装成缓冲流
         */
        String filePath = "E:\\code\\maven_code\\reflection-stu\\src\\main\\resources\\file003.txt";
        BufferedReader bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "utf8"));

        String line;

        while ((line = bufReader.readLine()) != null) {
            System.out.println(line);
        }

        bufReader.close();
    }
}

2、OutputStreamWriter

        输出转换流

public class OutputStreamWriterDemo {
    public static void main(String[] args) throws IOException {
        String filePath = "E:\\code\\maven_code\\reflection-stu\\src\\main\\resources\\file007.txt";
        String str = "helloworld";

        // 以字节流的形式打开文件,通过转换流转化为utf8编码的字符流,并包装成缓冲流
        BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), "utf8"));

        bufWriter.write(str);

        bufWriter.close();
    }
}

九、打印流

1、PrintStream

        字节打印流

public class PrintStreamDemo {
    public static void main(String[] args) throws IOException {

        PrintStream out = System.out;

        //  默认打印到标准输出(显示器) println底层就是write
        out.println("hello");
        out.write("hadoop".getBytes(StandardCharsets.UTF_8));

        // 设置输出路径
        System.setOut(new PrintStream("E:\\code\\maven_code\\reflection-stu\\src\\main\\resources\\file008.txt"));
        System.out.println("flink");

        out.close();
    }
}

2、PrintWriter

        字符打印流

public class PrintWriterDemo {
    public static void main(String[] args) throws FileNotFoundException {

        PrintWriter pWriter = new PrintWriter(System.out);
        PrintWriter fWriter = new PrintWriter("E:\\code\\maven_code\\reflection-stu\\src\\main\\resources\\file009.txt");

        pWriter.println("spark");
        fWriter.write("spark");

        pWriter.close();
        fWriter.close();
    }
}

十、Properties类

1、配置格式

        键=值

        键值对不需要有空格,值不需要用引号起来。默认类型是String

public class PropertiesDemo {
    public static void main(String[] args) throws IOException {

        String filePath = "E:\\code\\maven_code\\reflection-stu\\src\\main\\resources\\config.properties";
        String filePathOut = "E:\\code\\maven_code\\reflection-stu\\src\\main\\resources\\config2.properties";

        Properties properties = new Properties();
        properties.load(new FileReader(filePath));

        // 打印所有的配置文件
        properties.list(System.out);

        // 获取单个配置
        System.out.println(properties.getProperty("id"));

        // 修改配置
        // 注意保存中文时,是中文的 unicode 码值
        properties.setProperty("id", "172.25.88.115");

        // 保存配置文件
        properties.store(new FileWriter(filePathOut),null);

    }
}

注:笔记整理于B站韩顺平老师JAVA IO流课程,致谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值