-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileUtility.java
62 lines (54 loc) · 2.14 KB
/
FileUtility.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
import java.io.*;
public class FileUtility {
private static void write_into_file(String filename, String text, boolean append) throws IOException {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(filename, append))) {
bw.write(text);
}
}
public static void write_into_file(String filename, String text) throws IOException {
write_into_file(filename, text, false);
}
public static void append_into_file(String filename, String text) throws IOException {
write_into_file(filename, text, true);
}
public static String read_file(String filename) throws Exception {
InputStream in = new FileInputStream(filename);
InputStreamReader out = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(out);
String line;
String res = "";
while ((line = reader.readLine()) != null) {
res += line + "\n";
}
return res;
}
public static byte[] read_by_bytes(String filename) throws Exception {
File file = new File(filename);
try (FileInputStream fis = new FileInputStream(file)) {
byte[] bytes = new byte[(int) file.length()];
fis.read(bytes);
return bytes;
}
}
public static void write_by_bytes(String filename, byte[] data) throws IOException {
File file = new File(filename);
FileOutputStream fop = new FileOutputStream(file);
fop.write(data);
fop.flush();
fop.close();
}
public static void write_object_to_file(Serializable object, String filename) throws Exception {
try (FileOutputStream fos = new FileOutputStream(filename);
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
oos.writeObject(object);
}
}
public static Object read_file_as_object(String filename){
Object obj = null;
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename))){
obj = in.readObject();
} finally{
return obj;
}
}
}