cant't write data in csv file

934 Views Asked by At

I am beginner in java and I am try to write data in csv file using apache.poi library. but csvPrinter can't write data in csv file.

URL dir = getClass().getClassLoader().getResource("csv/input.csv");
File file = new File(dir.getFile());
FileWriter fileWriter = new FileWriter(file);
CSVPrinter csvPrinter = new CSVPrinter(fileWriter, csvFileFormat);
CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(",");
csvPrinter.print(data);

EDIT

This is code.

FileWriter fileWriter = null;
    CSVPrinter csvPrinter = null;
    CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(",");
    Logger logger = LoggerFactory.getLogger(this.getClass());

    {
        try{
            URL dir = getClass().getClassLoader().getResource("csv/input.csv");
            File file = new File(dir.getFile());
            fileWriter = new FileWriter(file);
            csvPrinter = new CSVPrinter(fileWriter, csvFileFormat);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

    @Override
    public void write(List<? extends String[]> messages) throws Exception {

        String[] data = messages.iterator().next();
        csvPrinter.print(data);
        for (String singleData : data) {
            System.out.println(singleData);
        }
//      csvwriter.close();
    }
0

There are 0 best solutions below