Home > Java > javaTutorial > body text

Sample code for reading and writing CSV files using OpenCSV in Java

WBOY
Release: 2023-12-20 14:51:49
Original
719 people have browsed it

在 Java 中使用 OpenCSV 进行 CSV 文件读写的实例

Example of using OpenCSV to read and write CSV files in Java

CSV (Comma-Separated Values) is a commonly used format for storing and transmitting tabular data. In Java, we can use the OpenCSV library to easily read and write CSV files. This article will introduce how to use OpenCSV to read and write CSV files and provide a simple example.

First, we need to introduce the OpenCSV library into the project. You can find OpenCSV in the Maven Central repository and add the following dependency in your pom.xml file:

<dependency>
    <groupId>com.opencsv</groupId>
    <artifactId>opencsv</artifactId>
    <version>5.5.2</version>
</dependency>
Copy after login

The first step is to read the CSV file. We can do this using the CSVReader class. First, we need to create a CSVReader object and specify the path to the CSV file. Then, use the readAll() method to read all the data in the file into a List object.

import com.opencsv.CSVReader;

import java.io.FileReader;
import java.io.IOException;
import java.util.List;

public class CSVReaderExample {
    public static void main(String[] args) {
        try {
            CSVReader reader = new CSVReader(new FileReader("path/to/csv/file.csv"));
            List<String[]> data = reader.readAll();

            for (String[] row : data) {
                for (String cell : row) {
                    System.out.print(cell + "    ");
                }
                System.out.println();
            }

            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Copy after login

In the above example, we first create a CSVReader object, and then use the readAll() method to read the data in the CSV file into a List object named data . Next, we print out all the data by looping through each row and cell of the data object.

Next, we will introduce how to use OpenCSV to write CSV files. We can do this using the CSVWriter class. First, we need to create a CSVWriter object and specify the path to the CSV file we want to write. We can then use the writeAll() method to write a List object to the file.

Here is an example of writing data to a CSV file:

import com.opencsv.CSVWriter;

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

public class CSVWriterExample {
    public static void main(String[] args) {
        try {
            CSVWriter writer = new CSVWriter(new FileWriter("path/to/csv/file.csv"));

            List<String[]> data = new ArrayList<>();
            data.add(new String[]{"Name", "Age", "City"});
            data.add(new String[]{"John", "25", "New York"});
            data.add(new String[]{"Jane", "30", "London"});

            writer.writeAll(data);

            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Copy after login

In the above example, we first create a CSVWriter object and specify the path to the CSV file to be written. Next, we create a List object named data that contains the data to be written. In this example, we write a header row (containing name, age, and city) and two rows of specific data. Then, we use the writeAll() method to write the data in the data object to the file.

The above is a simple example of using OpenCSV to read and write CSV files in Java. By using OpenCSV, we can easily read and write CSV files, reducing tedious operations and the amount of code. I hope this article can help you better understand and use OpenCSV.

The above is the detailed content of Sample code for reading and writing CSV files using OpenCSV in Java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template