-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCsvGenerator.java
124 lines (106 loc) · 3.22 KB
/
CsvGenerator.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
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;
public class CsvGenerator {
public CsvGenerator() {
}
public int listtoCsv(LinkedList<Dentist> list) {
int count = 0;
System.out.println("list size : " + list.size());
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
Calendar cal = Calendar.getInstance();
String fileName = dateFormat.format(cal.getTime());
FileWriter writer = null;
try {
writer = new FileWriter("Findadentist.ada.org_list_" + fileName + ".csv");
writer.append("ProfileUrl");
writer.append(",");
writer.append("Name");
writer.append(",");
writer.append("phone");
writer.append(",");
writer.append("Website");
writer.append(",");
writer.append("Address");
writer.append(",");
writer.append("Payment_options");
writer.append(",");
writer.append("Gender");
writer.append(",");
writer.append("Specialties");
writer.append(",");
writer.append("Language");
writer.append(",");
writer.append("Education");
writer.append(",");
writer.append("Practice_description");
writer.append(",");
writer.append("\n");
System.out.println(" -- out size-- "+ list.size());
if(list.size() > 0) {
Iterator<Dentist> it = list.iterator();
while (it.hasNext()) {
Dentist dentist = (Dentist) it.next();
try {
System.out.println(dentist.getProfileUrl());
System.out.println(dentist.getName());
writer.append(csvformatDevider(dentist.getProfileUrl()));
writer.append(",");
writer.append(csvformatDevider(dentist.getName()));
writer.append(",");
writer.append("["+dentist.getPhone()+"]");
writer.append(",");
writer.append(csvformatDevider(dentist.getWebsite()));
writer.append(",");
writer.append(csvformatDevider(dentist.getAddress()));
writer.append(",");
writer.append(csvformatDevider(dentist.getPayment_options()));
writer.append(",");
writer.append(csvformatDevider(dentist.getInsurance()));
writer.append(",");
writer.append(csvformatDevider(dentist.getGender()));
writer.append(",");
writer.append(csvformatDevider(dentist.getSpecialties()));
writer.append(",");
writer.append(csvformatDevider(dentist.getLanguage()));
writer.append(",");
writer.append(csvformatDevider(dentist.getEducation()));
writer.append(",");
writer.append(csvformatDevider(dentist.getPractice_description()));
writer.append("\n");
count++;
}catch (IOException e) {
//..
}
}
}
} catch (IOException e) {
System.out.println(" csv g Error : " + e.getMessage());
}finally {
try {
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return count;
}
private String csvformatDevider(String text) {
String newText;
try {
newText = text.replaceAll("[\\t\\n\\r]", " ");
if (newText.contains(","))
if (!newText.startsWith("\"") && !newText.endsWith("\""))
newText = "\"" + newText + "\"";
}catch (Exception e) {
return "";
}
return newText;
}
}