1 |
48028
|
claudio.at
|
package eu.dnetlib.data.collector.plugins.excel;
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* Created by miriam on 10/05/2017.
|
5 |
|
|
*/
|
6 |
|
|
import java.io.BufferedWriter;
|
7 |
|
|
import java.io.FileOutputStream;
|
8 |
|
|
import java.io.IOException;
|
9 |
|
|
import java.io.OutputStreamWriter;
|
10 |
|
|
import java.util.ArrayList;
|
11 |
|
|
import org.apache.commons.csv.CSVPrinter;
|
12 |
|
|
import org.apache.commons.csv.CSVFormat;
|
13 |
|
|
|
14 |
|
|
public class CSVFileWriter {
|
15 |
|
|
private static final String NEW_LINE_SEPARATOR = "\n";
|
16 |
|
|
|
17 |
|
|
private Object [] file_header ;
|
18 |
|
|
private ArrayList<ArrayList<String>> projects = new ArrayList<ArrayList<String>>();
|
19 |
|
|
|
20 |
|
|
public void setHeader(String[] header){
|
21 |
|
|
this.file_header = header;
|
22 |
|
|
}
|
23 |
|
|
|
24 |
|
|
public void addProject(ArrayList<String> project) {
|
25 |
|
|
projects.add(project);
|
26 |
|
|
|
27 |
|
|
}
|
28 |
|
|
|
29 |
|
|
public void writeFile(String csv_file_path){
|
30 |
|
|
BufferedWriter writer = null;
|
31 |
|
|
CSVPrinter csvFilePrinter = null;
|
32 |
|
|
|
33 |
|
|
CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);
|
34 |
|
|
|
35 |
|
|
try{
|
36 |
|
|
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csv_file_path),"UTF-8"));
|
37 |
|
|
|
38 |
|
|
csvFilePrinter = new CSVPrinter(writer,csvFileFormat);
|
39 |
|
|
csvFilePrinter.printRecord(file_header);
|
40 |
|
|
|
41 |
|
|
for(ArrayList<String> project:projects){
|
42 |
|
|
csvFilePrinter.printRecord(project);
|
43 |
|
|
}
|
44 |
|
|
}catch(Exception e){
|
45 |
|
|
e.printStackTrace();
|
46 |
|
|
}finally{
|
47 |
|
|
try{
|
48 |
|
|
writer.flush();
|
49 |
|
|
writer.close();
|
50 |
|
|
csvFilePrinter.close();
|
51 |
|
|
}catch(IOException ioe){
|
52 |
|
|
ioe.printStackTrace();
|
53 |
|
|
}
|
54 |
|
|
}
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
}
|