csv CSV file processing in various languages Reading and writing in Java


Example

Below example shows ways to read and write csv file without any third party libraries.

Write CSV

public void writeToCsvFile(List<String[]> thingsToWrite, String separator, String fileName){
    try (FileWriter writer = new FileWriter(fileName)){
        for (String[] strings : thingsToWrite) {
            for (int i = 0; i < strings.length; i++) {
                writer.append(strings[i]);
                if(i < (strings.length-1))
                    writer.append(separator);
            }
            writer.append(System.lineSeparator());
        }
        writer.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Read CSV

// Allows to define custom separator
public List<String[]> readFromCsvFile(String separator, String fileName){
    try (BufferedReader reader = new BufferedReader(new FileReader(fileName))){
        List<String[]> list = new ArrayList<>();
        String line = "";
        while((line = reader.readLine()) != null){
            String[] array = line.split(separator);
            list.add(array);
        }
        return list;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }  
}

There are also some pre-compiled third party libraries which provide convenient ways to parse csv files. Below are some examples of such libraries.

OpenCSV

OpenCSV is considered very simple to use and provides flexible functionalities while parsing CSV files

/** Reading CSV **/
// Allows varied parameters through constructors to define quote character, number of lines to skip, etc.
try(CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), separator)){
        List<String[]> = reader.readAll();
        // Do something with the data
}

/** Writing CSV **/
List<String[]> listToWrite= //fetch the list of string array to write;
try(CSVWriter writer = new CSVWriter(new FileWriter(fileName), separator)){
    writer.writeAll(listToWrite);
    writer.flush();
}

/** Dumping database records to CSV **/
// Initialize CSVWriter and fetch resultSet from database ...
    writer.writeAll(resultSet, includeColumnNames); 

OpenCSV also allowes binding the records directly to JavaBeans. for more information refer official documentation here.

Other known libraries include SuperCSV and CommonsCSV which provide some advanced functionalities as-well. Refer to official documentation for more information.