15.IO流

1. File类 (java.io.File)

文件和目录文件名的抽象表达形式

File类的实例化
File(File parent, String child)
根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
File(String pathname)
通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
File(String parent, String child)
根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。
parent为目录,child为文件全名
2. IO流概述

IO流是程序对数据进行输入 输出 流的操作

I:Input 输入 O:Output 输出

3. IO流分类

输入流 输出流

字节流 字符流

节点流 处理流

字节流
输入流 输出流
InputStream 输入流父类 OutputStream 输出流父类
FileInputStream 字节输入流 节点 FileOutputStream 字节输出流 节点
BufferedInputStream 缓冲区输入流 高级 BufferedOutputStream 缓冲区输出流 高级
ObjectInputStream 对象输入流 高级 ObjectOutputStream 对象输出流 高级

推荐使用高级流

高级流特点:操作方便,效率更高

字符流
输入流 输出流
FileReader FileWriter
BufferedReader BufferedWriter
4. 流的使用

流的使用步骤

  1. 创建流

  2. 读写

  3. 关流

// JDK写法 自动关流
try(/*创建流并实例化*/){/*进行读写操作*/}
5. Properties类(java.util.Properties)

Properties类表示一个持久的属性集,Properties可以保存在流中,或者从流中加载,属性列表中每个键对应的值都是一个字符串

  1. 如何读取properties文件中的数据
new Properties().load(properties文件);
  1. 通过key获取properties文件中的value
getProperty(key);
1. I O流 对应 读 写
2. 在文件路径中 \ 会被识别为转义符号,所以需要用 / 或 \\
posted @ 2021-05-13 21:33  因心光  阅读(36)  评论(0编辑  收藏  举报