forked from greenrobot/essentials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileUtils.java
149 lines (132 loc) · 4.9 KB
/
FileUtils.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
/*
* Copyright (C) 2014 Markus Junginger, greenrobot (http://greenrobot.de)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.greenrobot.common.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.zip.Checksum;
/**
* Utils for dealing with files.
*
* @author Markus
*/
public class FileUtils {
public static byte[] readBytes(File file) throws IOException {
FileInputStream is = new FileInputStream(file);
return IoUtils.readAllBytesAndClose(is);
}
public static void writeBytes(File file, byte[] content) throws IOException {
OutputStream out = new FileOutputStream(file);
try {
out.write(content);
} finally {
IoUtils.safeClose(out);
}
}
public static String readUtf8(File file) throws IOException {
return readChars(file, "UTF-8");
}
public static String readChars(File file, String charset) throws IOException {
Reader reader = new InputStreamReader(new FileInputStream(file), charset);
return IoUtils.readAllCharsAndClose(reader);
}
public static void writeUtf8(File file, CharSequence text) throws IOException {
writeChars(file, "UTF-8", text);
}
public static void writeChars(File file, String charset, CharSequence text) throws IOException {
Writer writer = new OutputStreamWriter(new FileOutputStream(file), charset);
IoUtils.writeAllCharsAndClose(writer, text);
}
/** Copies a file to another location. */
public static void copyFile(File from, File to) throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream(from));
try {
OutputStream out = new BufferedOutputStream(new FileOutputStream(to));
try {
IoUtils.copyAllBytes(in, out);
} finally {
IoUtils.safeClose(out);
}
} finally {
IoUtils.safeClose(in);
}
}
/** Copies a file to another location. */
public static void copyFile(String fromFilename, String toFilename) throws IOException {
copyFile(new File(fromFilename), new File(toFilename));
}
/** To read an object in a quick & dirty way. Prepare to handle failures when object serialization changes! */
public static Object readObject(File file) throws IOException,
ClassNotFoundException {
FileInputStream fileIn = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(fileIn));
try {
return in.readObject();
} finally {
IoUtils.safeClose(in);
}
}
/** To store an object in a quick & dirty way. */
public static void writeObject(File file, Object object) throws IOException {
FileOutputStream fileOut = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(fileOut));
try {
out.writeObject(object);
out.flush();
// Force sync
fileOut.getFD().sync();
} finally {
IoUtils.safeClose(out);
}
}
/** @return MD5 digest (32 characters). */
public static String getMd5(File file) throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream(file));
try {
return IoUtils.getMd5(in);
} finally {
IoUtils.safeClose(in);
}
}
/** @return SHA-1 digest (40 characters). */
public static String getSha1(File file) throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream(file));
try {
return IoUtils.getSha1(in);
} finally {
IoUtils.safeClose(in);
}
}
public static void updateChecksum(File file, Checksum checksum) throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream(file));
try {
IoUtils.updateChecksum(in, checksum);
} finally {
IoUtils.safeClose(in);
}
}
}