DataSaver.java (4474B) download
1package nl.isygameclient.util;
2
3import java.io.*;
4import java.nio.file.Files;
5import java.nio.file.Path;
6import java.nio.file.Paths;
7import java.util.Random;
8import java.util.Set;
9import java.util.stream.Collectors;
10import java.util.stream.Stream;
11
12public class DataSaver {
13
14 public static final String TEMP_PATH = "./data/temp/";
15 private final String path;
16 private final String fileName;
17 private final String filePath;
18
19 public DataSaver() {
20 this(TEMP_PATH, randomString(7) + ".csv" );
21 }
22
23 public DataSaver(String path, String fileName) {
24 this.path = path;
25 this.fileName = fileName;
26 filePath = path + fileName;
27 this.createDirectories();
28 }
29
30 /**
31 * Using the path provided in the constructor this function created the necessary directories
32 * in case they are not present.
33 */
34 public void createDirectories() {
35 File file = new File(path);
36 if (file.exists()) {
37 return;
38 }
39 if (file.isFile()) {
40 System.err.println("Path provided is not a directory: \n" + path);
41 System.exit(-1);
42 }
43
44 boolean result = file.mkdirs();
45 if (result) {
46 System.out.println("Created directories for path:\n " + path);
47 } else {
48 System.err.println("Failed to create directories for path:\n" + path);
49 }
50
51 }
52
53 /**
54 * Given a String containing data. This function will store the data inside a csv file
55 * of which the filepath has been provided in the constructor.
56 * @param data to be stored inside a csv file.
57 */
58 public void saveData(String data) {
59 try {
60 BufferedWriter output = new BufferedWriter(new FileWriter(filePath, true));
61 output.write(data);
62 output.newLine();
63 output.close();
64 } catch (IOException e) {
65 e.printStackTrace();
66 }
67 }
68
69 /**
70 * Given a directory containing csv files. This function will merge the contents into a singular csv file
71 * of which the filepath has been provided in the constructor.
72 * @param directoryPath to the directory containing csv files that need merging.
73 */
74 public void mergeFiles(String directoryPath) {
75 Set<String> files = getTempFiles(directoryPath);
76
77 assert files != null;
78 for (String file : files) {
79 System.out.println(directoryPath + file);
80 try {
81 BufferedReader bf = new BufferedReader(new FileReader(filePath));
82 bf.readLine();
83 String line = bf.readLine();
84
85 while (line != null) {
86 saveData(line);
87 line = bf.readLine();
88 }
89 bf.close();
90 } catch (IOException e) {
91 e.printStackTrace();
92 }
93 }
94 }
95
96 /**
97 * Given a path to a directory containing files.
98 * @param directoryPath containing files.
99 * @return A Set containing the file paths of all the files in the given directory.
100 */
101 private static Set<String> getTempFiles(String directoryPath) {
102 try (Stream<Path> stream = Files.list(Paths.get(directoryPath))) {
103 return stream
104 .filter(file -> !Files.isDirectory(file))
105 .map(Path::getFileName)
106 .map(Path::toString)
107 .collect(Collectors.toSet());
108 } catch (IOException e) {
109 e.printStackTrace();
110 }
111 return null;
112 }
113
114 /**
115 * Given a length this function returns a random string of said length consisting of capital letters, and numbers.
116 * @param length of the random string to be returned.
117 * @return String consisting of random capital letters A-Z, and numbers 0-9;
118 */
119 public static String randomString(int length) {
120 String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
121 StringBuilder sb = new StringBuilder();
122 Random random = new Random();
123
124 for (int i = 0; i < length; i++) {
125 int index = random.nextInt(alphabet.length());
126 char randomChar = alphabet.charAt(index);
127 sb.append(randomChar);
128 }
129 return sb.toString();
130 }
131
132 public String getPath() {
133 return path;
134 }
135
136 public String getFileName() {
137 return fileName;
138 }
139
140 public String getFilePath() {
141 return filePath;
142 }
143}