Repository navigation
Expand file tree
/
Copy pathFileUtil.java
More file actions
179 lines (165 loc) · 3.86 KB
/
Copy pathFileUtil.java
File metadata and controls
179 lines (165 loc) · 3.86 KB
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
package bms.core.common.util;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 文件操作工具类
*
* @author huangyong
* @since 1.0
*/
public class FileUtil {
private static final Logger logger = LoggerFactory
.getLogger(FileUtil.class);
/**
* 创建目录
*/
public static File createDir(String dirPath) {
File dir;
try {
dir = new File(dirPath);
if (!dir.exists()) {
FileUtils.forceMkdir(dir);
}
} catch (Exception e) {
logger.error("创建目录出错!", e);
throw new RuntimeException(e);
}
return dir;
}
/**
* 创建文件
*/
public static File createFile(String filePath) {
File file;
try {
file = new File(filePath);
File parentDir = file.getParentFile();
if (!parentDir.exists()) {
FileUtils.forceMkdir(parentDir);
}
} catch (Exception e) {
logger.error("创建文件出错!", e);
throw new RuntimeException(e);
}
return file;
}
/**
* 复制目录(不会复制空目录)
*/
public static void copyDir(String srcPath, String destPath) {
try {
File srcDir = new File(srcPath);
File destDir = new File(destPath);
if (srcDir.exists() && srcDir.isDirectory()) {
FileUtils.copyDirectoryToDirectory(srcDir, destDir);
}
} catch (Exception e) {
logger.error("复制目录出错!", e);
throw new RuntimeException(e);
}
}
/**
* 复制文件
*/
public static void copyFile(String srcPath, String destPath) {
try {
File srcFile = new File(srcPath);
File destDir = new File(destPath);
if (srcFile.exists() && srcFile.isFile()) {
FileUtils.copyFileToDirectory(srcFile, destDir);
}
} catch (Exception e) {
logger.error("复制文件出错!", e);
throw new RuntimeException(e);
}
}
/**
* 删除目录
*/
public static void deleteDir(String dirPath) {
try {
File dir = new File(dirPath);
if (dir.exists() && dir.isDirectory()) {
FileUtils.deleteDirectory(dir);
}
} catch (Exception e) {
logger.error("删除目录出错!", e);
throw new RuntimeException(e);
}
}
/**
* 删除文件
*/
public static void deleteFile(String filePath) {
try {
File file = new File(filePath);
if (file.exists() && file.isFile()) {
FileUtils.forceDelete(file);
}
} catch (Exception e) {
logger.error("删除文件出错!", e);
throw new RuntimeException(e);
}
}
/**
* 重命名文件
*/
public static void renameFile(String srcPath, String destPath) {
File srcFile = new File(srcPath);
if (srcFile.exists()) {
File newFile = new File(destPath);
boolean result = srcFile.renameTo(newFile);
if (!result) {
throw new RuntimeException("重命名文件出错!" + newFile);
}
}
}
/**
* 将字符串写入文件
*/
public static void writeFile(String filePath, String fileContent) {
OutputStream os = null;
Writer w = null;
try {
FileUtil.createFile(filePath);
os = new BufferedOutputStream(new FileOutputStream(filePath));
w = new OutputStreamWriter(os, "UTF-8");
w.write(fileContent);
w.flush();
} catch (Exception e) {
logger.error("写入文件出错!", e);
throw new RuntimeException(e);
} finally {
try {
if (os != null) {
os.close();
}
if (w != null) {
w.close();
}
} catch (Exception e) {
logger.error("释放资源出错!", e);
}
}
}
/**
* 获取真实文件名(去掉文件路径)
*/
public static String getRealFileName(String fileName) {
return FilenameUtils.getName(fileName);
}
/**
* 判断文件是否存在
*/
public static boolean checkFileExists(String filePath) {
return new File(filePath).exists();
}
}