提交 | 用户 | 时间
|
1ac2bc
|
1 |
package cn.stylefeng.guns.workflow.core.util; |
懒 |
2 |
|
|
3 |
import java.io.*; |
|
4 |
import java.nio.ByteBuffer; |
|
5 |
import java.nio.MappedByteBuffer; |
|
6 |
import java.nio.channels.FileChannel; |
|
7 |
import java.nio.channels.FileChannel.MapMode; |
|
8 |
|
|
9 |
/** |
|
10 |
* 文件处理 |
|
11 |
* |
|
12 |
* @author fengshuonan |
|
13 |
* @Date 2019/8/7 23:14 |
|
14 |
*/ |
|
15 |
public class FileUtil { |
|
16 |
|
|
17 |
|
|
18 |
/** |
|
19 |
* 获取文件大小 返回 KB 保留3位小数 没有文件时返回0 |
|
20 |
* |
|
21 |
* @param filepath 文件完整路径,包括文件名 |
|
22 |
* @return |
|
23 |
*/ |
|
24 |
public static Double getFilesize(String filepath) { |
|
25 |
File backupath = new File(filepath); |
|
26 |
return Double.valueOf(backupath.length()) / 1000.000; |
|
27 |
} |
|
28 |
|
|
29 |
/** |
|
30 |
* 创建目录 |
|
31 |
* |
|
32 |
* @param destDirName目标目录名 |
|
33 |
* @return |
|
34 |
*/ |
|
35 |
public static Boolean createDir(String destDirName) { |
|
36 |
File dir = new File(destDirName); |
|
37 |
if (!dir.getParentFile().exists()) { //判断有没有父路径,就是判断文件整个路径是否存在 |
|
38 |
return dir.getParentFile().mkdirs(); //不存在就全部创建 |
|
39 |
} |
|
40 |
return false; |
|
41 |
} |
|
42 |
|
|
43 |
/** |
|
44 |
* 删除文件 |
|
45 |
* |
|
46 |
* @param filePathAndName String 文件路径及名称 如c:/fqf.txt |
|
47 |
* @param fileContent String |
|
48 |
* @return boolean |
|
49 |
*/ |
|
50 |
public static void delFile(String filePathAndName) { |
|
51 |
try { |
|
52 |
String filePath = filePathAndName; |
|
53 |
filePath = filePath.toString(); |
|
54 |
java.io.File myDelFile = new java.io.File(filePath); |
|
55 |
myDelFile.delete(); |
|
56 |
} catch (Exception e) { |
|
57 |
System.out.println("删除文件操作出错"); |
|
58 |
e.printStackTrace(); |
|
59 |
} |
|
60 |
} |
|
61 |
|
|
62 |
/** |
|
63 |
* 读取到字节数组0 |
|
64 |
* |
|
65 |
* @param filePath //路径 |
|
66 |
* @throws IOException |
|
67 |
*/ |
|
68 |
public static byte[] getContent(String filePath) throws IOException { |
|
69 |
File file = new File(filePath); |
|
70 |
long fileSize = file.length(); |
|
71 |
if (fileSize > Integer.MAX_VALUE) { |
|
72 |
System.out.println("file too big..."); |
|
73 |
return null; |
|
74 |
} |
|
75 |
FileInputStream fi = new FileInputStream(file); |
|
76 |
byte[] buffer = new byte[(int) fileSize]; |
|
77 |
int offset = 0; |
|
78 |
int numRead = 0; |
|
79 |
while (offset < buffer.length |
|
80 |
&& (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) { |
|
81 |
offset += numRead; |
|
82 |
} |
|
83 |
// 确保所有数据均被读取 |
|
84 |
if (offset != buffer.length) { |
|
85 |
throw new IOException("Could not completely read file " + file.getName()); |
|
86 |
} |
|
87 |
fi.close(); |
|
88 |
return buffer; |
|
89 |
} |
|
90 |
|
|
91 |
/** |
|
92 |
* 读取到字节数组1 |
|
93 |
* |
|
94 |
* @param filePath |
|
95 |
* @return |
|
96 |
* @throws IOException |
|
97 |
*/ |
|
98 |
public static byte[] toByteArray(String filePath) throws IOException { |
|
99 |
|
|
100 |
File f = new File(filePath); |
|
101 |
if (!f.exists()) { |
|
102 |
throw new FileNotFoundException(filePath); |
|
103 |
} |
|
104 |
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length()); |
|
105 |
BufferedInputStream in = null; |
|
106 |
try { |
|
107 |
in = new BufferedInputStream(new FileInputStream(f)); |
|
108 |
int buf_size = 1024; |
|
109 |
byte[] buffer = new byte[buf_size]; |
|
110 |
int len = 0; |
|
111 |
while (-1 != (len = in.read(buffer, 0, buf_size))) { |
|
112 |
bos.write(buffer, 0, len); |
|
113 |
} |
|
114 |
return bos.toByteArray(); |
|
115 |
} catch (IOException e) { |
|
116 |
e.printStackTrace(); |
|
117 |
throw e; |
|
118 |
} finally { |
|
119 |
try { |
|
120 |
in.close(); |
|
121 |
} catch (IOException e) { |
|
122 |
e.printStackTrace(); |
|
123 |
} |
|
124 |
bos.close(); |
|
125 |
} |
|
126 |
} |
|
127 |
|
|
128 |
/** |
|
129 |
* 读取到字节数组2 |
|
130 |
* |
|
131 |
* @param filePath |
|
132 |
* @return |
|
133 |
* @throws IOException |
|
134 |
*/ |
|
135 |
public static byte[] toByteArray2(String filePath) throws IOException { |
|
136 |
File f = new File(filePath); |
|
137 |
if (!f.exists()) { |
|
138 |
throw new FileNotFoundException(filePath); |
|
139 |
} |
|
140 |
FileChannel channel = null; |
|
141 |
FileInputStream fs = null; |
|
142 |
try { |
|
143 |
fs = new FileInputStream(f); |
|
144 |
channel = fs.getChannel(); |
|
145 |
ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size()); |
|
146 |
while ((channel.read(byteBuffer)) > 0) { |
|
147 |
// do nothing |
|
148 |
// System.out.println("reading"); |
|
149 |
} |
|
150 |
return byteBuffer.array(); |
|
151 |
} catch (IOException e) { |
|
152 |
e.printStackTrace(); |
|
153 |
throw e; |
|
154 |
} finally { |
|
155 |
try { |
|
156 |
channel.close(); |
|
157 |
} catch (IOException e) { |
|
158 |
e.printStackTrace(); |
|
159 |
} |
|
160 |
try { |
|
161 |
fs.close(); |
|
162 |
} catch (IOException e) { |
|
163 |
e.printStackTrace(); |
|
164 |
} |
|
165 |
} |
|
166 |
} |
|
167 |
|
|
168 |
/** |
|
169 |
* Mapped File way MappedByteBuffer 可以在处理大文件时,提升性能 |
|
170 |
* |
|
171 |
* @param filename |
|
172 |
* @return |
|
173 |
* @throws IOException |
|
174 |
*/ |
|
175 |
public static byte[] toByteArray3(String filePath) throws IOException { |
|
176 |
|
|
177 |
FileChannel fc = null; |
|
178 |
RandomAccessFile rf = null; |
|
179 |
try { |
|
180 |
rf = new RandomAccessFile(filePath, "r"); |
|
181 |
fc = rf.getChannel(); |
|
182 |
MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0, |
|
183 |
fc.size()).load(); |
|
184 |
//System.out.println(byteBuffer.isLoaded()); |
|
185 |
byte[] result = new byte[(int) fc.size()]; |
|
186 |
if (byteBuffer.remaining() > 0) { |
|
187 |
// System.out.println("remain"); |
|
188 |
byteBuffer.get(result, 0, byteBuffer.remaining()); |
|
189 |
} |
|
190 |
return result; |
|
191 |
} catch (IOException e) { |
|
192 |
e.printStackTrace(); |
|
193 |
throw e; |
|
194 |
} finally { |
|
195 |
try { |
|
196 |
rf.close(); |
|
197 |
fc.close(); |
|
198 |
} catch (IOException e) { |
|
199 |
e.printStackTrace(); |
|
200 |
} |
|
201 |
} |
|
202 |
} |
|
203 |
|
|
204 |
} |