-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtility.java
More file actions
158 lines (137 loc) · 4.5 KB
/
FileUtility.java
File metadata and controls
158 lines (137 loc) · 4.5 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
package utilities.files;
import java.io.*;
import java.util.LinkedList;
import java.util.List;
public class FileUtility {
public static boolean deleteFile(String fileName) {
return (new File(fileName)).delete();
}
public static String readFile(InputStream inputStream) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
int ch;
try {
while ((ch = inputStream.read()) != -1) {
stringBuilder.append((char) ch);
}
} finally {
close(inputStream);
}
return stringBuilder.toString();
}
public static String readFile(File file) throws IOException {
return readFile(new FileInputStream(file));
}
public static String readFile(String fileName) throws IOException {
return readFile(new FileInputStream(fileName));
}
public static void writeToFile(FileWriter fileWriter, String content) throws IOException {
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(content);
bufferedWriter.flush();
} finally {
close(bufferedWriter);
close(fileWriter);
}
}
public static void writeToFile(File file, String content) throws IOException {
if (!file.exists()) {
file.createNewFile();
}
writeToFile(new FileWriter(file, true), content);
}
public static void writeToFile(String fileName, String content) throws IOException {
File file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
writeToFile(new FileWriter(fileName, true), content);
}
public static void copyFile(FileInputStream fileInputStream, FileOutputStream fileOutputStream) throws IOException {
try {
byte[] buf = new byte[1024];
int i;
while ((i = fileInputStream.read(buf)) != -1)
fileOutputStream.write(buf, 0 , i);
} finally {
close(fileInputStream);
close(fileOutputStream);
}
}
public static void copyFile(File input, File output) throws IOException {
if (!input.exists())
return;
if (!output.exists())
output.createNewFile();
copyFile(new FileInputStream(input), new FileOutputStream(output));
}
public static void copyFile(String inputFileName, String outputFileName) throws IOException {
copyFile(new FileInputStream(inputFileName), new FileOutputStream(outputFileName));
}
public static void close(Closeable closeable) {
if (null != closeable) {
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static List<String> readFileLines(InputStream inputStream) throws IOException {
List<String> lines = new LinkedList<>();
StringBuilder stringBuilder = new StringBuilder();
int ch;
try {
while ((ch = inputStream.read()) != -1) {
if (ch == '\n') {
lines.add(stringBuilder.toString());
stringBuilder = new StringBuilder();
} else {
stringBuilder.append((char) ch);
}
}
} finally {
close(inputStream);
}
return lines;
}
public static List<String> readFileLines(File file) throws IOException {
return readFileLines(new FileInputStream(file));
}
public static List<String> readFileLines(String fileName) throws IOException {
return readFileLines(new FileInputStream(fileName));
}
public static Integer[][] readNumberInLines(InputStream inputStream) throws IOException {
List<Integer[]> lines = new LinkedList<>();
StringBuilder stringBuilder = new StringBuilder();
String[] line;
Integer[] n;
int ch;
try {
while ((ch = inputStream.read()) != -1) {
if (ch == '\n') {
line = stringBuilder.toString().trim().split("\\s*(=>|,|\\s)\\s*");
n = new Integer[line.length];
for (int i = 0; i < line.length; ++i) {
n[i] = Integer.parseInt(line[i]);
}
lines.add(n);
stringBuilder = new StringBuilder();
} else {
stringBuilder.append((char) ch);
}
}
} finally {
close(inputStream);
}
Integer[][] res = {};
return lines.toArray(res);
}
public static Integer[][] readNumberInLines(File file) throws IOException {
return readNumberInLines(new FileInputStream(file));
}
public static Integer[][] readNumberInLines(String fileName) throws IOException {
return readNumberInLines(new FileInputStream(fileName));
}
}