I am trying to read a HashMap and write to a CSV file. I am using CSVPrinter, but my method is not returning a CSV file.
This is the code I use: Edited Code
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
public class SJV{
public File writeCSV(Map featureSet) throws IOException{
CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);
Map<String, Integer> map = featureSet;
File temp = File.createTempFile("tempfile", ".tmp");
FileWriter fs = new FileWriter(temp);
CSVPrinter csvFilePrinter = new CSVPrinter(fs, csvFileFormat);
//csvFilePrinter.printRecord(FILE_HEADER);
csvFilePrinter.printRecord(map.keySet().toArray());
csvFilePrinter.printRecord(map.values().toArray());
csvFilePrinter.close();
fs.close();
return temp;
}
}
This is the test I have:
public void testWriteCSV() throws IOException {
SJV sj = new SJV();
HashMap<String, Integer> hmap = new HashMap<String, Integer>();
hmap.put("Feature1", 1);
hmap.put("Feature2", 2);
File fs = sj.writeCSV(hmap);
if (fs.exists()){
Scanner input = new Scanner(fs );
while (input.hasNextLine())
{
System.out.println(input.nextLine());
}
input.close();
}
I really don't understand why the method is not returning a CSV file.
sj? Is anything written? Any errors?