-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataIO.java
44 lines (40 loc) · 1.41 KB
/
DataIO.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
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class DataIO {
public static List<String[]> importData(String filePath) {
List<String[]> dataList = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
String[] data = line.split(",");
dataList.add(data);
}
} catch (IOException e) {
e.printStackTrace();
}
return dataList;
}
public static void exportData(List<String[]> dataList, String filePath) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
for (String[] data : dataList) {
String line = String.join(",", data);
writer.write(line);
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// Import Data
List<String[]> importedData = importData("input.csv");
System.out.println("Imported data:");
for (String[] data : importedData) {
System.out.println(String.join(", ", data));
}
// Export Data
exportData(importedData, "output.csv");
System.out.println("Data exported successfully.");
}
}