img

文件

概念

imgimg

常用文件操作

创建

img

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
// 1. new File(String pathname)
@Test
public void create01() {
String pathname = "e:\\news1.txt";
File file = new File(pathname);
try {
file.createNewFile(); // 内存写入磁盘
System.out.println("文件创建成功..");
} catch (IOException e) {
e.printStackTrace();
}
}

// 2. new File(File parent, String child)
@Test
public void create02() {
File parentFile = new File("e:\\");
String fileName = "news2.txt";
File file = new File(parentFile, fileName);
try {
file.createNewFile();
System.out.println("文件创建成功..");
} catch (IOException e) {
e.printStackTrace();
}
}

获取

imgimg

目录操作

  • java将目录也看做文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 删除文件
public void m1() {
String filePath = "e:\\news1.txt";
File file = new File(filePath);
if (file.exists()) {
if (file.delete()) {
System.out.println(file.getName() + "删除成功"); // news1.txt删除成功
} else {
System.out.println(file.getName() + "删除失败");
}
} else {
System.out.println("文件不存在...");
}
}

img

IO流的原理及流的分类

概念

imgimg

分类

img

体系

img

字节流img

FileInputStream

  • 如果是读取中文

    • 一个中文占3个字节
    • 一次读一个字节 出现乱码

img

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
// 乱码版本
@Test
public void readFile01() {
String filePath = "e:\\hello.txt";
int readData = 0;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
// 读取
while ((readData = fileInputStream.read()) != -1) {
System.out.print((char) readData);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 一定要关闭
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

// 优化 read(byte[] b)
@Test
public void m2() {
String filePath = "e:\\hello.txt";
int readData = 0;
// 字节数组
byte[] buf = new byte[8];
int readLen = 0;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
// 读取
// 返回-1 结束
// 如果读取正常返回实际读取字节数
while ((readLen = fileInputStream.read(buf)) != -1) {
System.out.println(new String(buf, 0, readLen));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 一定要关闭
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

FileOutPutstream

  1. 追加和覆盖

img

  1. 写入

imgimg

拷贝文件

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
@Test
public void m1() {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
String filePath = "C:\\Users\\cyt\\Pictures\\Saved Pictures\\jay.gif";
String destFilePath = "E:\\cpjay.gif"; // 可以修改文件名
byte[] buf = new byte[1024];
int readLen = 0;
try {
// 创建流
fileInputStream = new FileInputStream(filePath);
fileOutputStream = new FileOutputStream(destFilePath);
while ((readLen = fileInputStream.read(buf)) != -1) {
// 一边读一边写
fileOutputStream.write(buf, 0, readLen);
}
System.out.println("拷贝成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭流
try {
if (fileInputStream != null) {
fileInputStream.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

字符流

FileReader

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void main(String[] args) {
String filePath = "e:\\hello.txt";
int data = 0; // 存储
FileReader fileReader = null;
try {
fileReader = new FileReader(filePath);
while (true) {
if (!((data = fileReader.read()) != -1)) break;
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {

}
}
  • 高效率

img

FileWriter

imgimg

字节流和处理流

基本概念

img

  • 分类记忆

    • 有Stream结尾是字节流
      • File Byte Piped 开头
    • Reader结尾是字符流
      • File char Piped String 开头

img

  • 处理流构造函数可以加入Writer的子类
  • 修饰器模式

img

区别和联系

img

处理流

BufferedReader

  • 文本末尾返回null
  • 按字符读取 不能读取二进制文件 图片 视频
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class BufferedReader_ {
public static void main(String[] args) throws Exception {
String filePath = "e:\\hello.txt";
// 创建BufferedReader
BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
// 读取
String line; // 按行读取效率高
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
// 关闭流 只需要关闭外层 底层自动关闭了FileReader
bufferedReader.close();
}
}

BufferedWriterimg

1
2
3
4
5
6
7
8
9
10
public class BufferedWrite_ {
public static void main(String[] args) throws Exception {
String filePath = "e:\\ok.txt";
BufferedWriter bufferedWrite = new BufferedWriter(new FileWriter(filePath));
for (int i = 0; i < 10; i++) {
bufferedWrite.write("hello!!!\n");
}
bufferedWrite.close();
}
}

Buffered 字符拷贝

  • 不能操作二进制文件
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
public class BufferedCopy_ {
public static void main(String[] args) {
String srcFilePath = "e:\\hello.txt";
String destFilePath = "e:\\desthello.txt";
BufferedReader br = null;
BufferedWriter bw = null;
String line;
try {
br = new BufferedReader(new FileReader(srcFilePath));
bw = new BufferedWriter(new FileWriter(destFilePath));
// 读取
while ((line = br.readLine()) != null) {
// 读取一行 不带换行符
bw.write(line);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (bw != null) {
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

BufferedInputStream

img

BufferedOutputStream

img img

Buffered 字节拷贝

  • 既可以操作二进制文件 也可以操作文本文件
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
public class BufferedCopy02 {
public static void main(String[] args) {
String srcPath = "e:\\1.jpg";
String destPath = "e:\\jks.jpg";
BufferedInputStream bis = null;
BufferedOutputStream bos = null;

try {
bis = new BufferedInputStream(new FileInputStream(srcPath));
bos = new BufferedOutputStream(new FileOutputStream(destPath));
// 循环读取写入
byte[] buff = new byte[1024];
int readLine = 0;
while ((readLine = bis.read(buff)) != -1) {
bos.write(buff, 0, readLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

对象流✨img

序列化和反序列化 (Serializable)

img

结构

img

ObjectOutputStream序列化

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
public class ObjectOutStream {
public static void main(String[] args) throws Exception {
// 序列化后保存格式不是纯文本文件 按他的格式
String filepath = "e:\\data.txt";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filepath));
// 序列化
oos.write(100); // 自动装箱 int -> Integer
oos.writeBoolean(true); // boolean -> Boolean
oos.writeChar('a'); // char -> Character
oos.writeDouble(1.1); // double -> Double
oos.writeUTF("abed"); // String
// 保存dog对象
oos.writeObject(new Dog("旺财", 11));
oos.close();
System.out.println("序列化完成");
}
}

// 如果要实现序列化 必须实现serializable接口
class Dog implements Serializable {
String name;
int age;

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

ObjectInputStream反序列化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class ObjectInputStream_ {
public static void main(String[] args) throws IOException, ClassNotFoundException {
String filePath = "e:\\data.txt";
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));
// 1.反序列化顺序要和序列化顺序一致
System.out.println(ois.readInt());
System.out.println(ois.readBoolean());
System.out.println(ois.readChar());
System.out.println(ois.readDouble());
System.out.println(ois.readUTF());
Object dog = ois.readObject();
System.out.println("运行类型 = " + dog.getClass()); // 运行类型 = class com.cyt.file.outputstream.Dog
System.out.println("dog信息 = " + dog); // dog信息 = com.cyt.file.outputstream.Dog@70177ecd

// 保证Dog 类在同一个包 且是public 可引用
Dog dog2 = (Dog) dog;
System.out.println(dog2.getAge());
ois.close();
}
}

注意事项img

  • transient 短暂的转瞬即逝的

img

标准输入流输出流img

  • 传入in 相当于到键盘获取字符
  • out 输出到显示器

img

转换流(处理中文乱码)img

InputStreamReader

  • 字节转字符

img

  • 字节流先转为字符流

    • 再将其转为处理流Buffered更高效方便

imgimg

OutPutStream

img

打印流

img

  1. PrintStream
1
2
3
4
5
6
7
8
9
10
11
public class PrintStream_ {
public static void main(String[] args) throws FileNotFoundException {
PrintStream out = System.out;
// 1.默认输出到显示器
out.print("hello!");
out.close();
// 2.修改打印流位置
System.setOut(new PrintStream("e:\\f1.txt"));
System.out.println("hello!"); // 输出到 f1.txt 文件
}
}
  1. PrintWriter

img

输入流✨

输出流✨

Properties类

原始方法

1
2
3
4
5
6
7
8
9
10
11
public class properties01 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("src\\mysql.properties"));
String line = "";
while ((line = br.readLine()) != null) {
String[] data = line.split("=");
System.out.println(data[0] + "值是: " + data[1]);
}
br.close();
}
}

基本介绍

img

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class properties02 {
public static void main(String[] args) throws IOException {
// 1. 创建对象
Properties properties = new Properties();
// 2.加载指定配置文件
properties.load(new FileReader("src\\mysql.properties"));
// 3.把键值对显示到控制台 (传入打印流)
properties.list(System.out);

/*-- listing properties --
user=root
pwd=123456
ip=127.0.0.0*/

// 根据key获取对应的值
String user = properties.getProperty("user");
String pwd = properties.getProperty("pwd");
System.out.println("用户名: " + user); // 用户名: root
System.out.println("密码: " + pwd); // 密码: 123456
}
}

修改添加

1
2
3
4
5
6
7
8
9
10
11
12
public class properties03 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
// 创建
properties.setProperty("charset", "utf8");
properties.setProperty("user", "tom");
properties.setProperty("pwd", "123");
// 将k-v存储到文件 null处可写注释
properties.store(new FileOutputStream("src\\mysql2.properties"), null);

}
}

img