0%

IO流

IO流

文件

文件流

文件在程序中是以流的形式来操作的,从文件读取到内存称为输入流、从内存输出到文件称为输出流

常用操作
  • 创建文件
1
2
3
4
5
// 根据路径构建文件
new File(String pathname);
file.createNewFile();
// 根据父目录文件+子路径构建文件
new File(File parent, String pathname);
  • 获取文件相关的信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 调用相应的方法,得到对应信息
// getName、getAbsolutePath、getParent、length、exists、isFile、isDirectory
System.out.println("文件名字=" + file.getName());
System.out.println("文件绝对路径=" + file.getAbsolutePath());
System.out.println("文件父级目录=" + file.getParent());
System.out.println("文件大小(字节)=" + file.length());
System.out.println("文件是否存在=" + file.exists());
System.out.println("是不是一个文件=" + file.isFile());
System.out.println("是不是一个目录=" + file.isDirectory());
// 目录的操作和文件删除
// 创建多级目录
file.mkdirs();
// 创建一级目录
file.mkdir();

IO流原理及流的分类

I/O流用于数据处理、读写文件。数据的输入输出以流的方式进行。

按照数据操作单位可以分为字节流、字符流,按照数据的流向可以分为输入流、输出流,按照处理对象可以分为节点流和处理流。

ch19-IO流
ch19-IO流

输入流

InputStream抽象类
  • FileInputStream文件输入流:从文件读取数据。
1
2
3
4
5
6
7
8
9
10
11
// 从文件名创建输入流对象
FileInputStream f = new FileInputStream("C:/java/hello");
// 使用文件对象创建输入流对象
File f = new File("C:/java/hello");
FileInputStream in = new FileInputStream(f);
// 字节流,读入文件 返回文件内容
readData = in.read();
// 字符流,读入文件 返回字符长度
readLength = in.read(byte[] buff);
// 关闭文件流,释放资源
in.close();
  • BufferedInputStream缓冲字节输入流
  • ObjectInputStream对象字节输入流
Reader
  • FileReader
1
2
3
4
5
6
// 创建输入流对象
FileReader f = new FileReader(File/String);
// 读取字符
f.read();
// 读取字符到数组
f.read(char[]);

输出流

OutputStream
  • FileOutputStream:创建文件,并向文件中写入数据。
1
2
3
4
5
6
7
8
9
10
11
12
// 使用字符串类型的文件名来创建一个输出流对象
FileOutputStream f = new FileOutputStream("C:/java/hello");
// 使用一个文件对象来创建一个输出流来写文件
File f = new File("C:/java/hello");
FileOutputStream fOut = new FileOutputStream(f);
// 写入一个字节
fOut.write(char w);
// 写入字符串
String str = "hello,world";
fOut.write(int str.getBytes());
// 将指定数组指定长度的字节写到输出流
fOut.write(byte[] w);
  • ByteArrayOutputStream
  • DataOutputStream
Writer
  • FileWriter
1
2
3
4
5
6
// 覆盖模式,新建对象
FileWriter f = new FileWriter(File/String);
// 写入
f.write();
//注意close
f.close();

文件拷贝

1
2
3
4
5
6
7
8
9
10
// 创建文件的输入流,将文件读入到程序
// 创建文件的输出流,将读到的文件,写入指定的文件
String src = "e:\\koala.jpg";
String dest = "e:\\koalacopy.jpg";
FileInputStream In = new FileInputStream(src);
FileOutputStream Out = new FileOutputStream(dest);
// 将读到的内容写入文件,一边读一边写
while((readLen = In.read(buf)) != -1){
Out.write(buf);
}

节点流和处理流

节点流:可以从一个特定的数据源(存放数据的地方)读写数据。 可以对文件、数组、管道、字符串进行操作,和 数据源直接相关。

处理流:对节点流进行包装,使用了修饰器设计模式,为程序提供更强大的读写功能。

处理字符流
  • BufferedReader

属于字符流,按照字符来读取数据。

1
2
3
4
5
6
7
8
9
// 创建bufferedReader,本质上是包装节点流
BufferedReader br = new BufferedReader(new FileReader("E:\\text.txt"));
// 按行读取,读不到时候返回为空
String line;
while ((line = br.readLine()) != null){
System.out.println(line);
}
// 关闭外层流,底层会关闭节点流
br.close();
  • BufferedWriter
1
2
3
4
5
6
7
8
9
10
11
// 创建BufferedWriter
BufferedWriter br = new BufferedWriter(new FileWriter("E:\\write.txt"));
// new FileWriter(filePath, true); 以追加的方式写入
// new FileWriter(filePath) 以覆盖的方式写入
// 写入
br.write("HELLO 韩顺平");
// 插入换行
br.newLine();
br.write("HELLO 韩顺平");
// 关闭外层流
br.close();
  • Buffered拷贝

surround with快捷键:Ctrl+alt+T

处理字节流
  • BufferedInputStream

  • BufferedOutputStream
  • 字节流拷贝

对象处理流

希望在保存数据的同时,保存数据的类型。需要对基本数据类型或对象进行序列化和反序列化,实现Serializable(推荐)、Externalizable接口之一。

  • ObjectOutputStream
1
2
3
4
5
6
// 创建对象处理流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))
// 写入对象内容
oos.write();
// 关闭文件
oos.close();
  • ObjectInputStream
1
2
3
4
5
6
7
// 创建对象处理流
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));
// 按写入顺序读取内容
System.out.println(ois.readInt());
System.out.println(ois.readBoolean());
// 关闭文件
ois.close();

注意事项:

  1. 读写顺序一致
  2. 实现序列化或反序列化的对象需要实现接口
  3. 序列化的类中建议添加,SerialVersionUID,以提高版本兼容性
  4. 序列化时,默认将所有属性都序列化,除了static、transient修饰的成员
  5. 序列化时,属性的类型也需要实现序列化接口
  6. 序列化具有可继承性:如果某类实现了序列化,则其子类也默认实现序列化
标准输入输出流

标准输入:System.in

标准输出:System.out

转换流:将字节流包装成字符流

处理纯文本数据时,使用字符流效率更高,并且可以有效的解决中文问题。

乱码问题:没有指定编码方式

  • InputStreamReader
1
2
3
4
5
6
7
8
// 将InputStreamReader字节流转换为InputSteamReader字符流,并指定编码方式gbk
InputSteamReader isr = new InputStreamReader(new FileInputStream(filePath),'gbk');
// 把InputReader 传入 BufferedReader
BufferedReader br = new BufferedReader(isr);
// 读取
String s = br.readLine();
// 关闭外层流
br.close();
  • OutputStreamReader
1
System.out.println(next);
打印流:只有输出流,没有输入流
  • PrintStream-字节打印流
1
2
3
4
5
6
7
8
// 建立对象
PrintStream out = System.out;
// 打印,默认输出位置是标准shur
out.print(string);
// 修改打印流输出的位置/设备
System.setOut(new PrintStream(path));
// 关闭流
out.close();
  • PrintWriter-字符打印流
1
System.out.println(next);

Properties类

是专门用于读写配置文件的集合类

配置文件格式:键=值

  • 常用方法
1
2
3
4
5
// 加载配置文件键值对到Properties对象
// list:将数据显示到指定的设备
// getProperty 根据键获取数据
// setProperty设置键值对到Property对象
// store:将Property中的键值对存储到配置文件,保存信息到配置文件
  • 读取Properties文件
1
2
3
4
5
6
7
8
// 创建对象
Properties properties = new Properties();
// 加载指定配置的文件
properties.load(new FileReader(file));
// k-v显示控制台
properties.list(System.out);
//根据key 获取对应的值
String user = properties.getProperty("user");
  • 修改Properties文件
1
2
3
4
// 创建对象
Properties properties = new Properties();
// 如果是新的值就增加,如果存在就替换
properties.setProperty("charset", "utf8");