-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkWithIO.java
51 lines (46 loc) · 1.48 KB
/
WorkWithIO.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
import java.io.*;
import java.util.Collections;
import java.util.LinkedList;
public class WorkWithIO {
private static String[] readFile() {
try (BufferedReader in = new BufferedReader(
new FileReader("Results.txt"))) {
return in.readLine().split(" ");
} catch (IOException | NullPointerException e) {
return null;
}
}
public static LinkedList<Integer> read() {
LinkedList<Integer> list = new LinkedList<>();
String[] s = readFile();
if (s != null) {
for (String value : s) {
try {
list.add(Integer.parseInt(value));
} catch (NullPointerException e) {
list = new LinkedList<>(Collections.nCopies(10, 0));
break;
} catch (NumberFormatException ignored) {
}
}
return list;
}
return null;
}
private static void write(final String numbers) {
try (BufferedWriter out = new BufferedWriter(
new FileWriter("Results.txt"))) {
out.write(numbers);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void write(final LinkedList<Integer> list) {
StringBuilder str = new StringBuilder();
for (Integer integer : list) {
str.append(integer).append(" ");
}
String res = str.toString();
write(res);
}
}