流的概念

  • 概念:内存与存储设备之间传输数据的通道

  • 水借助管道传输;数据借助流传输

流的分类

按方向【重点】:

  • 输入流:将<存储设备>中的内容读入到<内存>中
  • 输出流:将<内存>中的内容写入到<存储设备>中

按单位:

  • 字节流:以字节为单位,可以读写所有数据
  • 字符流:以字符为单位,只能读写文本数据

按功能:

  • 节点流:具有实际传输数据的读写功能
  • 过滤流:在字节流的基础之上增强功能

字节流

字节流的父类(抽象类):

  • InputStream:字节输入流

  • OutputStream:字节输出流

文件字节流:

  • FileInputStream:

public int read(byte[] b) //从流中读取多个字节,将读到内容存入b数组,返回实际读到的字节数;如果达到文件的尾部,则返回-1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.io;

import java.io.FileInputStream;

/*
演示FileInputStream的使用
文件字节输入流
*/
public class Demo01 {
public static void main(String[] args) throws Exception{
//1.创建FileInputStream,并指定文件路径
FileInputStream fis = new FileInputStream("E:\\aaa.txt");
//2.读取文件
//fis.read();
//2.1单个字节读取
// int date = 0;
// while ((date = fis.read())!=-1){
// System.out.print((char)date);
// }
//2.2一次读取多个字节
byte[] buf = new byte[1024];
// int count = fis.read(buf);
// System.out.println(new String(buf));
// System.out.println(count);
//
// int count2 = fis.read(buf);
// System.out.println(new String(buf));
// System.out.println(count2);

int count = 0;
while ((count=fis.read(buf))!=-1){
System.out.println(new String(buf,0,count));
}
//3.关闭
fis.close();
System.out.println("执行完毕");
}
}
  • FileOutputStream:

public void write(byte[] b) //一次写多个字节,将b数组中所有字节,写入输出流

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.io;

import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

/*
演示FileOutputStream的使用
文件字节输出流
*/
public class Demo02 {
public static void main(String[] args) throws Exception{
//1.创建文件字节输出流
FileOutputStream fos = new FileOutputStream("E:\\bbb.txt",true);
//2.写入文件
// fos.write(97);
// fos.write('b');
// fos.write('c');
String string = "helloworld";
fos.write(string.getBytes());
//3.关闭
fos.close();
System.out.println("执行完毕");
}
}

字节流复制文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;

/*
使用文件字节流实现文件的复制
*/
public class Demo03 {
public static void main(String[] args) throws Exception{
//1.创建流
//1.1文件字节输入流
FileInputStream fis = new FileInputStream("E:\\001.jpg");
//1.2文件字节输出流
FileOutputStream fos = new FileOutputStream("E:\\002.jpg");
//2.一边读,一边写
byte[] buf = new byte[1024];
int count = 0;
while ((count=fis.read(buf))!=-1){
fos.write(buf,0,count);
}
//3.关闭
fis.close();
System.out.println("执行完毕");
}
}

字节缓冲流

缓冲流:BufferedInputStream/BufferedOutputStream

  • 提高IO效率,减少访问磁盘的次数
  • 数据存储在缓存区中,flush是将缓存区的内容写入文件中,也可以直接close

BufferedInputStream的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.io;

import java.io.BufferedInputStream;
import java.io.FileInputStream;

/*
使用字节缓冲流读取
BufferedInputStream
*/
public class Demo04 {
public static void main(String[] args) throws Exception{
//1.创建BufferedInputStream
FileInputStream fis = new FileInputStream("E:\\aaa.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
//2.读取
// int date = 0;
// while ((date=bis.read())!=-1){
// System.out.print((char)date);
// }
//
byte[] buf = new byte[1024];
int count = 0;
while ((count=bis.read(buf))!=-1){
System.out.println(new String(buf,0,count));
}
//3.关闭
bis.close();
}
}

BufferedOutputStream的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.io;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

/*
使用字节缓冲流写入文件
BufferedOutputStream
*/
public class Demo05 {
public static void main(String[] args) throws Exception{
//1.创建字节输出缓冲流
FileOutputStream fos = new FileOutputStream("E:\\buffer.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
//2.写入文件
for (int i = 0; i < 10; i++) {
bos.write("helloworld\r\n".getBytes());//写入8k缓冲区
bos.flush();//刷新到硬盘
}
//3.关闭(内部会调用flush方法)
bos.close();
}
}

对象流

序列化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.io;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/*
使用ObjectOutputStream实现对象的序列化
要求:序列化类必须要实现Serializable接口
*/
public class Demo06 {
public static void main(String[] args) throws Exception{
//1.创建对象流
FileOutputStream fos = new FileOutputStream("E:\\stu.bin");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//2.序列化(写入操作)
Student zhangsan = new Student("张三", 20);
oos.writeObject(zhangsan);
//3.关闭
oos.close();
System.out.println("序列化完毕");
}
}

/*
package com.io;

import java.io.Serializable;

/*
学生类
*/
public class Student implements Serializable {
private String name;
private int age;

public Student() {
}

public Student(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
*/

反序列化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.io;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

/*
使用ObjectInputStream实现反序列化(读取重构成对象)
*/
public class Demo07 {
public static void main(String[] args) throws Exception{
//1.创建对象流
FileInputStream fis = new FileInputStream("E:\\stu.bin");
ObjectInputStream ois = new ObjectInputStream(fis);
//2.读取文件(反序列化)
Student s =(Student) ois.readObject();
//3.关闭
ois.close();
System.out.println("执行完毕");
System.out.println(s.toString());
}
}

序列化与反序列化注意事项

  1. 序列化类必须要实现Serializable接口
  2. 序列化类中对象属性要求实现Serializable接口
  3. 序列化版本号ID,保证序列化的类和反序列化的类是同一个类
  4. 使用transient(瞬间的)修饰属性,这个属性不能序列化
  5. 静态属性不能序列化
  6. 序列化多个对象,可以借助集合实现

编码方式

image-20220411194118290

当编码方式和解码方式不一致时,会出现乱码

字符流

字符流抽象类

字符流的父类(抽象类):

  • Reader:字符输入流
  • Writer:字符输出流

文件字符流

FileReader:

  • public int read(char[] c) //从流中读取多个字符,将读到内容存入c数组,返回实际读到的字符数;如果达到文件的尾部,则返回-1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.io;

import java.io.FileReader;

/*
使用FileReader读取文件
*/
public class Demo08 {
public static void main(String[] args) throws Exception{
//1.创建FileReader 文件字符输入流
FileReader fr = new FileReader("E:\\hello.txt");
//2.读取
//2.1单个字符读取
// int date = 0;
// while ((date=fr.read())!=-1){//读取一个字符
// System.out.print((char)date);
// }
char[] buf = new char[1024];
int count = 0;
while ((count=fr.read(buf))!=-1) {
System.out.println(new String(buf,0,count));
}
//3.关闭
fr.close();
}
}

FileWriter:

  • public void write(String str) //一次写多个字符,将b数组中所有字符,写入输出流
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.io;

import java.io.FileWriter;

/*
使用FileWriter写入文件
*/
public class Demo09 {
public static void main(String[] args) throws Exception{
//1.创建FileWriter对象
FileWriter fw = new FileWriter("E:\\write.txt");
//2.写入
for (int i = 0; i < 10; i++) {
fw.write("java是世界上最好的语言\r\n");
fw.flush();
}
//3.关闭
fw.close();
System.out.println("执行完毕");
}
}

字符流复制文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.io;

import java.io.FileReader;
import java.io.FileWriter;

/*
使用FlieReader和FileWriter复制文本文件,不能复制图片或二进制文件
使用字节流复制任意文件。
*/
public class Demo10 {
public static void main(String[] args) throws Exception{
//1.创建FlieReader、FileWriter
FileReader fr = new FileReader("E:\\write.txt");
FileWriter fw = new FileWriter("E:\\write2.txt");
//2.读写
int date = 0;
while ((date=fr.read())!=-1) {
fw.write(date);
fw.flush();
}
//3.关闭
fr.close();
fw.close();
System.out.println("复制完毕 ");
}
}

字符缓冲流

缓冲流:BufferedReader/BufferedWriter

  • 高效读写
  • 支持输入换行符
  • 可一次写一行、读一行

BufferedReader的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.io;

import java.io.BufferedReader;
import java.io.FileReader;

/*
使用字符缓冲流读取文件
BufferedReader
*/
public class Demo11 {
public static void main(String[] args) throws Exception{
//1.创建缓冲流
FileReader fr = new FileReader("E:\\write.txt");
BufferedReader br = new BufferedReader(fr);
//2.读取
//2.1第一种方式
// char[] buf = new char[1024];
// int count = 0;
// while ((count=br.read(buf))!=-1) {
// System.out.println(new String(buf,0,count));
// }
//2.2第二种方式,一行一行的读取
String line = null;
while ((line= br.readLine())!=null) {
System.out.println(line);
}
//3.关闭
br.close();
}
}

BufferedWriter的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.io;

import java.io.BufferedWriter;
import java.io.FileWriter;

/*
演示BufferedWriter的使用
*/
public class Demo12 {
public static void main(String[] args) throws Exception{
//1.创建BufferedWriter的使用
FileWriter fw = new FileWriter("E:\\buffer.txt");
BufferedWriter bw = new BufferedWriter(fw);
//2.写入
for (int i = 0; i < 10; i++) {
bw.write("好好学习,天天向上");
bw.newLine();//写入一个换行符 windows \r\n Linux \n
bw.flush();
}
//3.关闭
bw.close();
System.out.println("执行完毕");
}
}

打印流

PrintWriter:

  • 封装了print() / println()方法,支持写入后换行
  • 支持数据原样打印
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.io;

import java.io.PrintWriter;

/*
演示PrintWriter的使用
*/
public class Demo13 {
public static void main(String[] args) throws Exception{
//1.创建打印流
PrintWriter pw = new PrintWriter("E:\\print.txt");
//2.打印
pw.println(97);
pw.println(true);
pw.println(3.14);
pw.println('a');
//3.关闭
pw.close();
System.out.println("执行完毕");
}
}

转换流

桥转换流:InputStreamReader / OutputStreamWriter

  • 可将字节流转换为字符流
  • 可设置字符的编码方式

InputStreamReader的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.io;

import java.io.FileInputStream;
import java.io.InputStreamReader;

/*
使用InputStreamReader读取文件,指定使用的编码
*/
public class Demo14 {
public static void main(String[] args) throws Exception{
//1.创建InputStreamReader对象
FileInputStream fis = new FileInputStream("E:\\write.txt");
InputStreamReader isr = new InputStreamReader(fis, "utf-8");
//2.读取文件
int date = 0;
while ((date=isr.read())!=-1) {
System.out.print((char)date);
}
//3.关闭
isr.close();
}
}

OutputStreamWriter的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.io;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

/*
使用OutputStreamWriter写入文件,使用指定的编码
*/
public class Demo15 {
public static void main(String[] args) throws Exception{
//1.创建OutputStreamWriter
FileOutputStream fos = new FileOutputStream("E:\\info.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
//2.写入
for (int i = 0; i < 10; i++) {
osw.write("我爱北京,我爱故乡\r\n");
osw.flush();
}
//3.关闭
osw.close();
System.out.println("执行成功");
}
}

File类

  • 概念:代表物理盘符中的一个文件或者文件夹
  • 方法:

文件(夹)操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.io;

import java.io.File;
import java.util.Date;

/*
File类的使用
1.分隔符
2.文件操作
3.文件夹操作
*/
public class Demo16 {
public static void main(String[] args) throws Exception {
//separator();
fileOpe();
}
//1.分隔符
public static void separator() {
System.out.println("路径分隔符"+ File.pathSeparator);
System.out.println("名称分隔符"+ File.separator);
}
//2.文件操作
public static void fileOpe() throws Exception{
//1.创建文件
File file = new File("E:\\file.txt");
System.out.println(file.toString());
if (!file.exists()) {
boolean b = file.createNewFile();
System.out.println("创建结果:"+b);
}

//2.删除文件
//2.1直接删除
//System.out.println("删除结果:"+file.delete());
//2.2使用jvm退出时删除
// file.deleteOnExit();
// Thread.sleep(5000);//休眠5秒钟

//3.获取文件信息
System.out.println("获取文件的绝对路径:"+file.getAbsolutePath());
System.out.println("获取路径:"+file.getPath());
System.out.println("获取文件名称:"+file.getName());
System.out.println("获取父目录:"+file.getParent());
System.out.println("获取文件长度:"+file.length());
System.out.println("文件创建时间:"+new Date(file.lastModified()).toLocaleString());

//4.判断
System.out.println("是否可写:"+file.canWrite());
System.out.println("是否是文件:"+file.isFile());
System.out.println("是否隐藏:"+file.isHidden());
}
//3.文件夹操作
public static void directoryOpe() throws Exception{
//1.创建文件夹
File dir = new File("E:\\aaa\\bbb\\ccc");
System.out.println(dir.toString());
//dir.mkdir();//只能创建单级目录
if (!dir.exists()) {
System.out.println("创建结果:" + dir.mkdirs());//创建多级目录
}
//2.删除文件夹
//2.1直接删除(只能删除空目录)
//System.out.println("删除结果:"+dir.delete());
//2.2使用jvm删除
dir.deleteOnExit();
Thread.sleep(5000);
//3.获取文件夹信息
System.out.println("获取绝对路径:"+dir.getAbsolutePath());
System.out.println("获取路径:"+dir.getPath());
System.out.println("获取文件夹名称:"+dir.getName());
System.out.println("获取父目录:"+dir.getParent());
System.out.println("获取创建时间:"+new Date(dir.lastModified()).toLocaleString());
//4.判断
System.out.println("是否是文件夹:"+dir.isDirectory());
System.out.println("是否隐藏:"+dir.isHidden());
//5.遍历文件夹
File dir2 = new File("E:\\图片");
String[] files = dir2.list();
System.out.println("=================================");
for (String string : files) {
System.out.println(string);
}
}
}

FileFilter接口

  • pubic interface FileFilter

boolean accept (File pathname)

  • 当调用File类中的listFiles()方法时,支持传入FileFilter接口实现类,对获取文件进行过滤,只有满足条件的文件才可出现在listFiles()的返回值中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.io;

import java.io.File;
import java.io.FileFilter;
import java.util.Date;

/*
File类的使用
1.分隔符
2.文件操作
3.文件夹操作
*/
public class Demo16 {
public static void main(String[] args) throws Exception {
//separator();
fileOpe();
}
//1.分隔符
public static void separator() {
System.out.println("路径分隔符"+ File.pathSeparator);
System.out.println("名称分隔符"+ File.separator);
}
//2.文件操作
public static void fileOpe() throws Exception{
//1.创建文件
File file = new File("E:\\file.txt");
System.out.println(file.toString());
if (!file.exists()) {
boolean b = file.createNewFile();
System.out.println("创建结果:"+b);
}

//2.删除文件
//2.1直接删除
//System.out.println("删除结果:"+file.delete());
//2.2使用jvm退出时删除
// file.deleteOnExit();
// Thread.sleep(5000);//休眠5秒钟

//3.获取文件信息
System.out.println("获取文件的绝对路径:"+file.getAbsolutePath());
System.out.println("获取路径:"+file.getPath());
System.out.println("获取文件名称:"+file.getName());
System.out.println("获取父目录:"+file.getParent());
System.out.println("获取文件长度:"+file.length());
System.out.println("文件创建时间:"+new Date(file.lastModified()).toLocaleString());

//4.判断
System.out.println("是否可写:"+file.canWrite());
System.out.println("是否是文件:"+file.isFile());
System.out.println("是否隐藏:"+file.isHidden());
}
//3.文件夹操作
public static void directoryOpe() throws Exception{
//1.创建文件夹
File dir = new File("E:\\aaa\\bbb\\ccc");
System.out.println(dir.toString());
//dir.mkdir();//只能创建单级目录
if (!dir.exists()) {
System.out.println("创建结果:" + dir.mkdirs());//创建多级目录
}
//2.删除文件夹
//2.1直接删除(只能删除空目录)
//System.out.println("删除结果:"+dir.delete());
//2.2使用jvm删除
dir.deleteOnExit();`
Thread.sleep(5000);
//3.获取文件夹信息
System.out.println("获取绝对路径:"+dir.getAbsolutePath());
System.out.println("获取路径:"+dir.getPath());
System.out.println("获取文件夹名称:"+dir.getName());
System.out.println("获取父目录:"+dir.getParent());
System.out.println("获取创建时间:"+new Date(dir.lastModified()).toLocaleString());
//4.判断
System.out.println("是否是文件夹:"+dir.isDirectory());
System.out.println("是否隐藏:"+dir.isHidden());
//5.遍历文件夹
File dir2 = new File("E:\\图片");
String[] files = dir2.list();
System.out.println("=================================");
for (String string : files) {
System.out.println(string);
}

System.out.println("=============FileFilter接口的使用");
File[] files2 = dir2.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
if (pathname.getName().endsWith(".jpg")) {
return true;
}
return false;
}
});
for (File file : files2) {
System.out.println(file.getName());
}
}
}

递归遍历和递归删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.io.List;

import java.io.File;

/*
案例1:递归遍历文件夹
案例1:递归删除文件夹
*/
public class ListDemo {
public static void main(String[] args) {
listDir(new File("E:\\aaa"));
deleteDir(new File("E:\\aaa"));
}
//案例1:递归遍历文件夹
public static void listDir(File dir) {
File[] files = dir.listFiles();
System.out.println(dir.getAbsolutePath());
if (files!=null&&files.length>0) {
for (File file : files) {
if (file.isDirectory()) {
listDir(file);//递归
}else {
System.out.println(file.getAbsolutePath());
}
}
}
}

//案例2:递归删除文件夹
public static void deleteDir(File dir) {
File[] files = dir.listFiles();
if (files!=null&&files.length>0) {
for (File file : files) {
if (file.isDirectory()) {
deleteDir(file);
}else {
//删除文件
System.out.println(file.getAbsolutePath()+"删除:"+file.delete());
}
}
}
System.out.println(dir.getAbsolutePath()+"删除:"+dir.delete());
}
}

补充:Properties

Properties:属性集合

特点:

  • 存储属性名和属性值
  • 属性名和属性值都是字符串类型
  • 没有泛型
  • 和流有关
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.Set;

/*
演示Properties集合的使用
*/
public class Demo17 {
public static void main(String[] args) throws Exception{
//1.创建集合
Properties properties = new Properties();
//2.添加数据
properties.setProperty("username","zhangsan");
properties.setProperty("age","20");
System.out.println(properties.toString());
//3.遍历
//3.1---keySet---
//3.2---entrySet---
//3.3---stringPropertyNames()---
Set<String> pronames = properties.stringPropertyNames();
for (String pro : pronames) {
System.out.println(pro+"===="+properties.getProperty(pro));
}
//4.和流有关的方法
//=====list====
PrintWriter pw = new PrintWriter("E:\\print.txt");
properties.list(pw);
pw.close();

//====2.store方法 保存====
FileOutputStream fos = new FileOutputStream("E:\\store.properties");
properties.store(fos,"注释");
fos.close();

//====3.load方法 加载====
Properties properties2 = new Properties();
FileInputStream fis = new FileInputStream("E:\\store.properties");
properties2.load(fis);
fis.close();
System.out.println(properties2.toString());
}
}