-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVFileWriter.java
More file actions
139 lines (109 loc) · 4.59 KB
/
Copy pathCSVFileWriter.java
File metadata and controls
139 lines (109 loc) · 4.59 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
package pom.kgoeltner;
/**
* Program to convert an JSONArray or ArrayList into a CSV file in src/main/resources
*
* @author Karl Goeltner
* @version February 22, 2020
*/
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
//import java.util.List;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
//import org.json.simple.parser.JSONParser;
public class CSVFileWriter {
// Delimiter used in CSV file
private static final String COMMA_DELIMITER = ",";
private static final String NEW_LINE_SEPARATOR = "\n";
// Method to convert JSONArray of users into csv file
public static void writeUsersCSV(String fileName, JSONArray users) {
FileWriter fileWriter = null;
JSONObject name = new JSONObject();
int cnt = 0;
try {
fileWriter = new FileWriter(fileName);
// Write a new student object list to the CSV file
for (Object user : users) {
// Store JSONObject of user
name = (JSONObject)user;
fileWriter.append(String.valueOf(cnt));
fileWriter.append(COMMA_DELIMITER);
fileWriter.append((String)name.get("name"));
fileWriter.append(NEW_LINE_SEPARATOR);
cnt++;
}
System.out.println("users.csv was created successfully !!!");
} catch (Exception e) {
System.out.println("Error in CsvFileWriter !!!");
e.printStackTrace();
} finally {
try {
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
System.out.println("Error while flushing/closing fileWriter !!!");
e.printStackTrace();
}
}
}
// Method to convert JSONArray of products into csv file
public static void writeProductsCSV(String fileName, JSONArray products) {
FileWriter fileWriter = null;
JSONObject product = new JSONObject();
int cnt = 1;
try {
fileWriter = new FileWriter(fileName);
// Write a new student object list to the CSV file
for (Object prod : products) {
// Store JSONObject of user
product = (JSONObject)prod;
fileWriter.append(String.valueOf(cnt));
fileWriter.append(COMMA_DELIMITER);
fileWriter.append((((String)product.get("word")).substring(0,1)).toUpperCase() + ((String)product.get("word")).substring(1));
fileWriter.append(NEW_LINE_SEPARATOR);
cnt++;
}
System.out.println("products.csv was created successfully !!!");
} catch (Exception e) {
System.out.println("Error in CsvFileWriter !!!");
e.printStackTrace();
} finally {
try {
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
System.out.println("Error while flushing/closing fileWriter !!!");
e.printStackTrace();
}
}
}
// Method to convert ArrayList of ratings into csv file
public static void writeRatingsCSV(String fileName, ArrayList<RATING> ratings) {
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(fileName);
// Write a new student object list to the CSV file
for (RATING rating : ratings) {
fileWriter.append(String.valueOf(rating.getName()));
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(String.valueOf(rating.getProduct()));
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(String.valueOf(rating.getRating()));
fileWriter.append(NEW_LINE_SEPARATOR);
}
System.out.println("ratings.csv was created successfully !!!");
} catch (Exception e) {
System.out.println("Error in CsvFileWriter !!!");
e.printStackTrace();
} finally {
try {
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
System.out.println("Error while flushing/closing fileWriter !!!");
e.printStackTrace();
}
}
}
}