-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileUtil.java
195 lines (182 loc) · 5.58 KB
/
FileUtil.java
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package cn.xanderye.util;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.Scanner;
import java.util.stream.Stream;
/**
* Created on 2020/9/1.
*
* @author XanderYe
*/
public class FileUtil {
/**
* 默认缓存大小
*/
public static final int DEFAULT_BUFFER_SIZE = 1024;
/**
* 默认中等缓存大小
*/
public static final int DEFAULT_MIDDLE_BUFFER_SIZE = 4096;
/**
* 默认大缓存大小
*/
public static final int DEFAULT_LARGE_BUFFER_SIZE = 8192;
/**
* 数据流末尾
*/
public static final int EOF = -1;
/**
* 复制单文件
* @param sourcePath 源文件路径
* @param targetPath 目标文件路径
* @return void
* @author XanderYe
* @date 2020/9/1
*/
public static void copyFile(String sourcePath, String targetPath) throws IOException {
try (FileInputStream fis = new FileInputStream(sourcePath);
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(targetPath);
BufferedOutputStream bos = new BufferedOutputStream(fos)) {
byte[] bytes = new byte[DEFAULT_BUFFER_SIZE];
int len;
while ((len = bis.read(bytes)) != EOF) {
bos.write(bytes, 0, len);
}
}
}
/**
* 复制文件夹
* @param sourcePath 源文件夹路径
* @param targetPath 目标文件夹路径
* @return void
* @author XanderYe
* @date 2020/9/1
*/
public static void copyDictionary(String sourcePath, String targetPath) throws IOException {
File sourceFile = new File(sourcePath);
if (!sourceFile.exists()) {
throw new FileNotFoundException();
}
File targetFile = new File(targetPath);
targetFile.mkdirs();
File[] fileList = sourceFile.listFiles();
if (fileList != null && fileList.length > 0) {
for (File f : fileList) {
if (f.isDirectory()) {
copyDictionary(f.getAbsolutePath(), targetPath + File.separator + f.getName());
} else {
copyFile(f.getAbsolutePath(), targetPath + File.separator + f.getName());
}
}
}
}
/**
* 删除文件夹
* @param filePath
* @return void
* @author XanderYe
* @date 2020/9/1
*/
public static void deleteDictionary(String filePath) throws IOException {
Path path = Paths.get(filePath);
try (Stream<Path> walk = Files.walk(path)) {
walk.sorted(Comparator.reverseOrder())
.forEach(FileUtil::deleteFile);
}
}
/**
* 删除文件
* @param filePath
* @return void
* @author XanderYe
* @date 2020/9/1
*/
private static void deleteFile(Path filePath) {
try {
Files.delete(filePath);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取文件编码
* @param filePath
* @return java.nio.charset.Charset
* @author XanderYe
* @date 2021/1/11
*/
public static Charset identifyCharset(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException();
}
Charset charset;
try (FileInputStream fis = new FileInputStream(file)) {
byte[] head = new byte[3];
fis.read(head);
//判断TXT文件编码格式
if (head[0] == -1 && head[1] == -2) {
//Unicode -1,-2,84
charset = Charset.forName("Unicode");
} else if (head[0] == -2 && head[1] == -1) {
//Unicode big endian -2,-1,0,84
charset = StandardCharsets.UTF_16;
} else if (head[0] == -17 && head[1] == -69 && head[2] == -65) {
//UTF-8 -17,-69,-65,84
charset = StandardCharsets.UTF_8;
} else {
//ANSI 84 = T
charset = Charset.forName("gb2312");
}
}
return charset;
}
/**
* 根据文件名获取文件格式
* @param name
* @return java.lang.String
* @author XanderYe
* @date 2021/8/16
*/
public static String getFileExt(String name) {
if (name == null) {
return null;
}
int dotIndex = name.lastIndexOf(".");
if (dotIndex > -1) {
return name.substring(dotIndex + 1);
}
return null;
}
/**
* 读取文本文件
* @param filePath
* @return java.lang.String
* @author XanderYe
* @date 2022/8/3
*/
public static String readFileString(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException();
}
if (file.isDirectory()) {
throw new IOException("目标文件是文件夹");
}
try (FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr)) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append(SystemUtil.getLineBreak());
}
return sb.toString();
}
}
}