1

I created a CsvBuilderService

CSVBuilderService<Vector<Object>> builder = new CSVBuilderService<Vector<Object>>();

Then I created ByeArrayOutputStream:

  ByteArrayOutputStream os = (ByteArrayOutputStream)builder.buildCsvToStream(tableDataMatrixTemp);

tableDataMatrixTemp is a Vector< Object >.

Now, how can I print that > on a CSV and saving the csv on a specified path? (I don't want to download the file from web, only save it to a path).

2 Answers 2

1

Use FileOutputStream.

ByteArrayOutputStream os = (ByteArrayOutputStream)builder.buildCsvToStream(tableDataMatrixTemp);
try(FileOutputStream fos = new FileOutputStream("your-filename-goes-here")) {
   os.writeTo(fos);
}

Java 7+

Sign up to request clarification or add additional context in comments.

Comments

0

Below code snippet might help you !

 public void writeCSV() {

            // Delimiter used in CSV file
            private static final String NEW_LINE_SEPARATOR = "\n";

            // CSV file header
            private static final Object[] FILE_HEADER = { "Empoyee Name","Empoyee Code", "In Time", "Out Time", "Duration", "Is Working Day" };

            String fileName = "fileName.csv");
            List<Objects> objects = new ArrayList<Objects>();
            FileWriter fileWriter = null;
            CSVPrinter csvFilePrinter = null;

            // Create the CSVFormat object with "\n" as a record delimiter
            CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);

            try {
                fileWriter = new FileWriter(fileName);

                csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);

                csvFilePrinter.printRecord(FILE_HEADER);

                // Write a new student object list to the CSV file
                for (Object object : objects) {
                    List<String> record = new ArrayList<String>();

                    record.add(object.getValue1().toString());
                    record.add(object.getValue2().toString());
                    record.add(object.getValue3().toString());

                    csvFilePrinter.printRecord(record);
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    fileWriter.flush();
                    fileWriter.close();
                    csvFilePrinter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.