腾飞工作室

Java文件操作案例

总结常见的文件操作、流操作的用法

文件操作

常见流

1
2
3
InputStream : 是所有字节输入流的超类,一般使用它的子类:FileInputStream等,它能输出字节流;
InputStreamReader : 是字节流与字符流之间的桥梁,能将字节流输出为字符流,并且能为字节流指定字符集,可输出一个个的字符;
BufferedReader : 提供通用的缓冲方式文本读取,readLine读取一个文本行, 从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。

流操作

1
2
3
4
5
InputStream inputStream = new FileInputStream(new File('/Users/xxx/test.txt'))
//groovy 自动调用 close 关闭流
inputStream.withStream { it->
//对输入流操作
}

转换

String > InputStream

1
InputStream is = new ByteArrayInputStream(string.getBytes("UTF-8"));

InputStream > String

String > OutputStream

OutputStream > String

OutputStream > InputStream

1
2
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()
ByteArrayInputStream byteArrayInputStream =new ByteArrayInputStream(byteArrayOutputStream.toByteArray())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 将输入流持久化到文件
*
* @param file 目标文件
* @param inputStream 要持久化的输入流
* @param append true从头部开始覆盖目标文件内容,false 不覆盖在目标文件尾部追加
* @param bufferSize 使用的内存缓存区大小,推荐传入 8 * 1024
* @throws IOException
*/
public static void inputStreamToFile(File file, InputStream inputStream, boolean append, int bufferSize) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(file, append);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
byte[] data = new byte[bufferSize];
while (inputStream.read(data) != -1) {
bufferedOutputStream.write(data);
bufferedOutputStream.flush();
}
bufferedOutputStream.close();
fileOutputStream.close();
inputStream.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
70
71
72
73
74
75
import org.apache.commons.io.input.TeeInputStream;
import java.io.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
/**
* 流的复制,将一个InputStream变成多个InputStream
* <p>
* <code>
Shunt
.load(fileInputStream)
.pipo(new Consumer() {
@Override
public void accept(Object o) {
InputStream inputStream = (InputStream) o;
try {
IOUtil.inputStreamToFile(new File("d:/bz/1.csv"), inputStream, true, 8 * 1024);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}, new Consumer() {
@Override
public void accept(Object o) {
InputStream inputStream = (InputStream) o;
try {
IOUtil.inputStreamToFile(new File("d:/bz/2.csv"), inputStream, true, 8 * 1024);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
);
* </code>
* <p>
* Created by liutf on 2016/7/6 0006.
*/
public class Shunt {
InputStream inputStream;
public static Shunt load(InputStream inputStream) {
return new Shunt(inputStream);
}
private Shunt(InputStream inputStream) {
this.inputStream = inputStream;
}
private InputStream shunt(InputStream inputStream) throws IOException {
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream(in);
TeeInputStream tee = new TeeInputStream(inputStream, out, true);
this.inputStream = in;
return tee;
}
public Shunt pipo(Consumer<InputStream>... consumers) throws IOException {
for (Consumer<InputStream> consumer : consumers) {
InputStream br = shunt(inputStream);
CompletableFuture.runAsync(() -> {
try {
consumer.accept(br);
} catch (Exception e) {
e.printStackTrace();
}
});
}
return this;
}
}

本文出自 “腾飞工作室” 博客,请务必保留此出处:http://tfgzs.net/2017/11/06/Java文件操作案例/