IO操作工具类

在此用来记录一些日常开发中会用到的IO操作的代码

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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/**
* 部分来自网络
*/
public class IOUtil {

public IOUtil() {
}

public static void delete(File file) {
file.delete();
}

public static void delete(String file) {
(new File(file)).delete();
}

public static void mv(String source, String target) {
try {
Runtime.getRuntime().exec(String.format("mv %s %s", new Object[]{source, target}));
} catch (IOException var3) {
var3.printStackTrace();
}

}

public static void mv(File source, File target) {
target.deleteOnExit();
source.renameTo(target);
}

public static String readString(String file) throws IOException {
return readString(new File(file));
}

public static String readString(File file) throws IOException {
FileInputStream in = new FileInputStream(file);
String str = readString((InputStream) in);
in.close();
return str;
}

public static byte[] readBytes(InputStream in) throws IOException {
byte[] buf = new byte[1024];
ByteArrayOutputStream out = new ByteArrayOutputStream();
boolean c = false;

int c1;
while ((c1 = in.read(buf)) > 0) {
out.write(buf, 0, c1);
}

byte[] bytes = out.toByteArray();
out.close();
return bytes;
}

public static byte[] readBytes(String path) throws IOException {
FileInputStream in = new FileInputStream(path);
byte[] bytes = readBytes((InputStream) in);
in.close();
return bytes;
}

public static String readString(InputStream in) throws IOException {
byte[] bytes = readBytes(in);
return new String(bytes, "UTF-8");
}

public static void writeString(OutputStream out, String str) throws IOException {
out.write(str.getBytes());
}

public static void appendString(OutputStream out, String str) throws IOException {
out.write(str.getBytes());
}

public static void appendString(File file, String str) throws IOException {
FileOutputStream out = new FileOutputStream(file, true);
out.write(str.getBytes());
out.close();
}

public static void appendString(String file, String str) throws IOException {
appendString(new File(file), str);
}

public static void writeString(File file, String str) throws IOException {
FileOutputStream out = new FileOutputStream(file);
out.write(str.getBytes());
out.close();
}

public static void writeUTF8String(File file, String str) throws IOException {
OutputStreamWriter outw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
outw.write(str);
outw.close();
}

public static void writeUTF8String(String file, String str) throws IOException {
writeUTF8String(new File(file), str);
}

public static void writeString(String file, String str) throws IOException {
writeString(new File(file), str);
}

public static void copy(InputStream in, String target) throws IOException {
FileOutputStream out = null;

try {
out = new FileOutputStream(new File(target));
byte[] buf = new byte[10240];
boolean c = false;

int c1;
while ((c1 = in.read(buf)) > 0) {
out.write(buf, 0, c1);
}
} finally {
if (out != null) {
try {
out.flush();
out.close();
} catch (IOException var10) {
var10.printStackTrace();
}
}

}
}

public static void copy(String source, String target) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;

try {
in = new FileInputStream(new File(source));
out = new FileOutputStream(new File(target));
byte[] e = new byte[1024];
boolean c = false;

int c1;
while ((c1 = in.read(e)) > 0) {
out.write(e, 0, c1);
}

} finally {
if (in != null) {
try {
in.close();
} catch (IOException var20) {
var20.printStackTrace();
}
}

if (out != null) {
try {
out.flush();
out.close();
} catch (IOException var19) {
var19.printStackTrace();
}
}

}
}

public static boolean copyWithFileLock(String source, String target) {
FileInputStream in = null;
FileOutputStream out = null;
FileChannel fileChannel = null;
File targetFile = new File(target);
FileLock fileLock = null;
boolean hasBeenLocked = false;

try {
in = new FileInputStream(new File(source));
out = new FileOutputStream(targetFile, true);
//用源文件做锁,不然目标文件会被置空
fileChannel = out.getChannel();
fileLock = fileChannel.tryLock();
if (fileLock == null) {
hasBeenLocked = true;
fileLock = fileChannel.lock();
}

//由于是复制,所以复制一次就够了,等其他地方复制完毕,就返回
if (hasBeenLocked) {
return true;
}

out = new FileOutputStream(targetFile);
byte[] e = new byte[1024];
boolean c = false;

int c1;
while ((c1 = in.read(e)) > 0) {
out.write(e, 0, c1);
}

out.flush();
out.close();
return true;
} catch (FileNotFoundException var21) {
var21.printStackTrace();
return false;
} catch (IOException var22) {
var22.printStackTrace();
} finally {
if (fileLock != null) {
try {
fileLock.release();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException var20) {
var20.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException var19) {
var19.printStackTrace();
}
}


}
return false;
}

public static void serialize(Serializable obj, String file) throws FileNotFoundException, IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));

try {
oos.writeObject(obj);
} catch (IOException var11) {
var11.printStackTrace();
throw var11;
} finally {
try {
oos.close();
} catch (IOException var10) {
var10.printStackTrace();
}

}

}

public static Object unserialize(String file) throws Exception {
ObjectInputStream ois = null;

try {
ois = new ObjectInputStream(new FileInputStream(file));
Object var4 = ois.readObject();
return var4;
} catch (Exception var12) {
;
} finally {
try {
if (ois != null) {
ois.close();
}
} catch (IOException var11) {
var11.printStackTrace();
}

}

return null;
}

public static Object cloneObject(Object obj) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = null;

ObjectInputStream ois = null;
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(out);
oos.writeObject(obj);

in = new ByteArrayInputStream(out.toByteArray());
ois = new ObjectInputStream(in);
return ois.readObject();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (oos != null)
oos.close();
if (ois != null)
ois.close();
if (in != null)
in.close();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}

public static void saveToFile(InputStream in, String file) throws IOException {
File outputFile = new File(file);
outputFile.deleteOnExit();
outputFile.getParentFile().mkdirs();
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
byte[] buffer = new byte[2048];
int length = in.read(buffer);
while (length != -1) {
outputStream.write(buffer, 0, length);
length = in.read(buffer);
}
outputStream.flush();
outputStream.close();
}

public static void saveToFile(InputStream in, File file) throws IOException {
File outputFile = file;
outputFile.deleteOnExit();
outputFile.getParentFile().mkdirs();
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
byte[] buffer = new byte[2048];
int length = in.read(buffer);
while (length != -1) {
outputStream.write(buffer, 0, length);
length = in.read(buffer);
}
outputStream.flush();
outputStream.close();
}

/**
* 获得指定文件的byte数组
*/
public static byte[] getBytes(String filePath) {
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
byte[] b = new byte[(int) file.length()];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}

public static boolean deleteDirs(String themePath) {
LinkedList<File> themeLinkedList = new LinkedList<File>();
File themeDir = new File(themePath);
if (!themeDir.exists()) {
return true;
} else if (themeDir.isDirectory()) {
themeLinkedList.addAll(Arrays.asList(themeDir.listFiles()));
while (!themeLinkedList.isEmpty())
deleteContent(themeLinkedList.pollLast());
} else {
return themeDir.delete();
}
return true;
}

private static void deleteContent(File file) {
LinkedList<File> themeLinkedList = new LinkedList<File>();
if (file.isDirectory()) {
themeLinkedList.addAll(Arrays.asList(file.listFiles()));
while (!themeLinkedList.isEmpty()) {
File subFile = themeLinkedList.pollLast();
deleteContent(subFile);
}
}
file.delete();
}

public static void copyFile(String srcPath, String desPath) throws IOException {
File srcDir = new File(srcPath);
File desDir = new File(desPath);
if (!srcDir.exists()) {
return;
}
if (srcDir.isDirectory()) {
desDir.mkdirs();
File[] files = srcDir.listFiles();
for (int i = 0; i < files.length; i++) {
File file = files[i];
File des = new File(desDir, file.getName());
copyFile(file.getAbsolutePath(), des.getAbsolutePath());
}
} else {
desDir.getParentFile().mkdirs();
IOUtil.copy(srcPath, desPath);
}
}

/**
* 获取SDCARD的绝对路径
*
* @return Environment.getExternalStorageDirectory().getAbsolutePath()
*/
public static String getSdcardPath() {
return Environment.getExternalStorageDirectory().getAbsolutePath();
}

/**
* 获取文件夹大小
* @param file File实例
* @return long
*/
public static long getFolderSize(File file) {

long size = 0;
try {
java.io.File[] fileList = file.listFiles();
if (fileList != null) {

for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isDirectory()) {
size = size + getFolderSize(fileList[i]);

} else {
size = size + fileList[i].length();

}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return size;
}

/**
* 获取缓存路径
*
* @param context
* @return
*/
public static String getCachePath(Context context) {
String cachePath = null;

if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {

cachePath = context.getExternalCacheDir().getPath();

} else {
cachePath = context.getCacheDir().getPath();
}
return cachePath;
}
}

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×