[JAVA] csv file output with opencsv

The simplest form. I don't think it will be used as it is, but can it be used as a base?

The opencsv.jar version is 4.0 and the java version is 1.7.

CsvOutput.java


package com.test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.opencsv.CSVWriter;

public class CsvOutput {

	public static void main(String[] args) throws IOException {

		CSVWriter csvw = null;
		try {
            //Instance generation
			csvw = new CSVWriter(
	               new FileWriter(new File("c:\\test", "test.csv"))
	                , ",".charAt(0)
	                , "\"".charAt(0)
	                , "\"".charAt(0)
	                , "\r\n");
            //An array of String becomes one row of data
	        List<String[]> outStrList = new ArrayList<String[]>();
            //Define the number of items for the number of arrays
            //Actually, I don't think I'll write it like this ...
	        String[] outStr = new String[2];
	        outStr[0] = "Drinking too much";
	        outStr[1] = "Spit out";
	        outStrList.add(outStr);

	        //writing
	        csvw.writeAll(outStrList);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
                        //Close at the end
			if (csvw != null) {
				csvw.close();
			}
		}
	}
}

・ Output result

test.csv


-Output file contents
"Drinking too much","Spit out"

Recommended Posts

csv file output with opencsv
CSV output with Apache Commons CSV
Let's write Java file input / output with NIO
Customize the output with Wagby's CSV download function
[Java] Convert and import file values with OpenCSV
[Spring Batch] Output table data to CSV file
CSV import with BOM
Java CSV library "opencsv"
Output FizzBuzz with stream
I tried to read and output CSV with Outsystems
Output log to external file with slf4j + logback with Maven
How to request a CSV file as JSON with jMeter
Check CSV value with RSpec
iOS: File upload with SFTP
Output javadoc to word file
File upload with Spring Boot
Output multiplication table with Stream
Control log output with Doma2
Output XML tree to file
Output "Izumi Oishi" with dokojava
Supports 0 drop in CSV output
Csv output processing using super-csv
Output Rails Routes as csv
Output Excel with formulas with XlsMapper
I made a tool to output the difference of CSV file
Output characters like conversation with JavaFX
Minimal Rails with reduced file generation
Output test coverage with clover + gradle
Read dump file with Docker MySQL
[Ruby on Rails] CSV output function
Create an excel file with poi
Implement file download with Spring MVC
Log output to file in Java
Output PDF and TIFF with Java 8
EXCEL file update sample with JAVA