/
io.java
398 lines (283 loc) · 9.38 KB
/
io.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
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
package accessory;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
public abstract class io extends parent_static
{
public static final String STREAM = "stream";
public static final String ERROR_WRITE = _types.ERROR_FILE_WRITE;
public static final String ERROR_READ = _types.ERROR_FILE_READ;
public static final String ERROR_DELETE = _types.ERROR_FILE_DELETE;
public static void array_to_file(String path_, String[] vals_, boolean append_)
{
method_start();
if (!strings.is_ok(path_)) return;
if (!arrays.is_ok(vals_))
{
empty_file(path_);
return;
}
try (FileWriter writer = new FileWriter(path_, append_))
{
for (String val: vals_) { line_to_file(path_, val, append_, writer); }
}
catch (Exception e) { manage_error_io(ERROR_WRITE, e, path_); }
method_end();
}
public static String[] file_to_array(String path_) { return file_web_to_array(path_, true); }
public static String[] web_to_array(String url_) { return file_web_to_array(url_, false); }
public static ArrayList<HashMap<String, String>> file_to_hashmap(String path_, String[] cols_, String separator_, boolean normalise_, boolean ignore_first_)
{
method_start();
int tot = arrays.get_size(cols_);
if (tot < 1 || !paths.file_exists(path_)) return null;
ArrayList<HashMap<String, String>> output = new ArrayList<HashMap<String, String>>();
boolean is_first = true;
try (BufferedReader reader = get_reader(path_))
{
while (reader != null && reader.ready())
{
String line = get_line(reader);
if (is_first)
{
is_first = false;
if (ignore_first_) continue;
}
String[] vals = strings.split(separator_, line);
if (!arrays.is_ok(vals)) continue;
HashMap<String, String> item = new HashMap<String, String>();
for (int i = 0; i < tot; i++) { item.put(cols_[i], vals[i].trim()); }
output.add(item);
}
}
catch (Exception e)
{
output = null;
manage_error_io(ERROR_READ, e, path_);
}
if (output != null && arrays.get_size(output) == 0) output = null;
method_end();
return output;
}
public static void hashmap_to_file(String path_, ArrayList<HashMap<String, String>> vals_, String separator_, boolean cols_to_first_)
{
method_start();
if (!strings.is_ok(path_)) return;
if (!arrays.is_ok(vals_))
{
empty_file(path_);
return;
}
boolean is_first = true;
try (FileWriter writer = new FileWriter(path_, false))
{
for (HashMap<String, String> item: vals_)
{
if (is_first)
{
if (cols_to_first_) line_to_file(path_, arrays.to_string(arrays.get_keys_hashmap(item), separator_), false, writer);
is_first = false;
}
line_to_file(path_, arrays.to_string(arrays.get_values_hashmap(item), separator_), false, writer);
}
}
catch (Exception e) { manage_error_io(ERROR_WRITE, e, path_); }
method_end();
}
public static String file_to_string(String path_, boolean only_first_)
{
String output = strings.DEFAULT;
String[] lines = file_to_array(path_);
if (arrays.get_size(lines) > 0) output = (only_first_ ? lines[0] : arrays.lines_to_string(lines));
return output;
}
public static void line_to_file(String path_, String line_, boolean append_) { line_to_file(path_, line_, append_, null); }
public static void empty_file(String path_) { line_to_file(path_, "", false); }
//Ini files are assumed to contain text stored as key-value pairs, one per line.
//A descriptive example --> this is key: and all this, including :, is value.
public static HashMap<String, String> ini_to_array(String path_)
{
method_start();
HashMap<String, String> ini = null;
if (!strings.contains_end(paths.EXTENSION_INI, path_, true)) return ini;
String[] lines = file_to_array(path_);
if (!arrays.is_ok(lines)) return ini;
ini = new HashMap<String, String>();
for (String line: lines)
{
String[] temp = strings.split(misc.SEPARATOR_KEYVAL, line, true, true);
if (arrays.get_size(temp) < 2) continue;
String key = temp[0];
String value = temp[1];
if (!strings.is_ok(key) || !strings.is_ok(value)) continue;
ini.put(key.trim(), value.trim());
}
method_end();
return ini;
}
public static void ini_to_file(String path_, HashMap<String, String> ini_)
{
method_start();
if (!strings.is_ok(path_)) return;
if (!arrays.is_ok(ini_))
{
empty_file(path_);
return;
}
try (FileWriter writer = new FileWriter(path_, false))
{
for (Entry<String, String> item: ini_.entrySet())
{
String line = item.getKey() + misc.SEPARATOR_KEYVAL + item.getValue();
line_to_file(path_, line, false, writer);
}
}
catch (Exception e) { manage_error_io(ERROR_WRITE, e, path_); }
method_end();
}
public static void bytes_to_file(String path_, byte[] vals_)
{
method_start();
if (!strings.is_ok(path_)) return;
if (!arrays.is_ok(vals_))
{
empty_file(path_);
return;
}
try (FileOutputStream stream = new FileOutputStream(new File(path_))) { stream.write(vals_); }
catch (Exception e) { manage_error_io(ERROR_WRITE, e, path_); }
method_end();
}
public static byte[] file_to_bytes(String path_)
{
method_start();
byte[] output = null;
if (!paths.file_exists(path_)) return output;
try { output = Files.readAllBytes(Paths.get(path_)); }
catch (Exception e) { manage_error_io(ERROR_READ, e, path_); }
method_end();
return output;
}
public static void object_to_file(String path_, Object vals_)
{
method_start();
if (!strings.is_ok(path_)) return;
if (vals_ == null)
{
empty_file(path_);
return;
}
try (ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(path_))) { stream.writeObject(vals_); }
catch (Exception e) { manage_error_io(ERROR_WRITE, e, path_); }
method_end();
}
public static Object file_to_object(String path_)
{
method_start();
Object output = null;
if (!paths.file_exists(path_)) return output;
try (ObjectInputStream stream = new ObjectInputStream(new FileInputStream(path_))) { output = stream.readObject(); }
catch (Exception e) { manage_error_io(ERROR_WRITE, e, path_); }
method_end();
return output;
}
public static boolean delete_file(String path_)
{
method_start();
boolean output = false;
if (!paths.is_file(path_)) return output;
if (!paths.exists(path_)) return true;
try
{
(new File(path_)).delete();
output = true;
}
catch (Exception e) { manage_error_io(ERROR_DELETE, e, path_); }
method_end();
return output;
}
public static ArrayList<String> get_lines(String path_)
{
ArrayList<String> lines = null;
try { lines = get_lines(get_stream(path_), path_); }
catch (Exception e)
{
lines = null;
manage_error_io(ERROR_READ, e, path_);
}
return lines;
}
public static ArrayList<String> get_lines(InputStream input_) { return get_lines(input_, null); }
private static ArrayList<String> get_lines(InputStream input_, String path_)
{
if (input_ == null) return null;
ArrayList<String> lines = new ArrayList<String>();
try (BufferedReader reader = get_reader(input_))
{
while (reader.ready()) { lines.add(get_line(reader)); }
}
catch (Exception e)
{
lines = null;
manage_error_io(ERROR_READ, e, (strings.is_ok(path_) ? path_ : STREAM));
}
return lines;
}
private static String[] file_web_to_array(String path_url_, boolean is_file_)
{
method_start();
ArrayList<String> lines = null;
try { lines = (is_file_ ? get_lines(path_url_) : get_lines(new URL(path_url_).openStream(), path_url_)); }
catch (Exception e)
{
lines = null;
manage_error_io(ERROR_READ, e, path_url_);
}
String[] output = null;
if (arrays.is_ok(lines)) output = arrays.to_array(lines);
method_end();
return output;
}
private static void line_to_file(String path_, String line_, boolean append_, FileWriter writer_)
{
method_start();
if (!strings.is_ok(path_) || line_ == null) return;
try
{
Boolean is_new = false;
FileWriter writer = writer_;
if (writer == null)
{
if (!strings.is_ok(path_)) return;
is_new = true;
writer = new FileWriter(path_, append_);
}
writer.write(line_ + misc.NEW_LINE);
if (is_new) writer.close();
}
catch (Exception e) { manage_error_io(ERROR_WRITE, e, path_); }
method_end();
}
private static BufferedReader get_reader(String path_) throws Exception { return get_reader(get_stream(path_)); }
private static BufferedReader get_reader(InputStream stream_) { return (stream_ == null ? null : new BufferedReader(new InputStreamReader(stream_, strings.get_encoding()))); }
private static InputStream get_stream(String path_) throws Exception { return (paths.file_exists(path_) ? new FileInputStream(path_) : null); }
private static String get_line(BufferedReader reader_) throws Exception { return reader_.readLine().trim(); }
private static void manage_error_io(String type_, Exception e_, String path_)
{
HashMap<String, Object> info = new HashMap<String, Object>();
info.put(_keys.PATH, path_);
manage_error(type_, e_, info);
}
}