当生活像一首歌那样轻快流畅时,笑颜常开乃易事;而在一切事都不妙时仍能微笑的人,是真正的乐观。——威尔科克斯

曾经我们使用javaIO流复制文件时是这么写的

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
package com.ruben;

import java.io.*;
import java.util.Optional;

/**
* @ClassName: FileDemo
* @Description: 我还没有写描述
* @Date: 2021/1/10 0010 21:38
* *
* @author: <achao1441470436@gmail.com>
* @version: 1.0
* @since: JDK 1.8
*/
public class FileDemo {
private static final String FILE_PATH = "D:/file/files/ps/2077.png";
private static final String TARGET_PATH = "D:/file/files/ps/1977.png";

public static void main(String[] args) {
InputStream dataInputStream = null;
OutputStream output = null;
try {
dataInputStream = new FileInputStream(FILE_PATH);
output = new FileOutputStream(TARGET_PATH);
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
Optional.ofNullable(output).ifPresent(outputStream -> {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
});
Optional.ofNullable(dataInputStream).ifPresent(inputStream -> {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
}

可以看到在finally里我们关闭流的时候要写一大堆,非常麻烦

现在推荐使用这种写法

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
package com.ruben;

import java.io.*;
import java.util.Optional;

/**
* @ClassName: FileDemo
* @Description: 我还没有写描述
* @Date: 2021/1/10 0010 21:38
* *
* @author: <achao1441470436@gmail.com>
* @version: 1.0
* @since: JDK 1.8
*/
public class FileDemo {
private static final String FILE_PATH = "D:/file/files/ps/2077.png";
private static final String TARGET_PATH = "D:/file/files/ps/1977.png";

public static void main(String[] args) {
try (
InputStream dataInputStream = new FileInputStream(FILE_PATH);
OutputStream output = new FileOutputStream(TARGET_PATH)
) {
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

使用try()catch{}写法,可以自动关闭流