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>
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 Listd8e50004b3852d85b72500fd6e9ac257 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(); } } }
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 Listd8e50004b3852d85b72500fd6e9ac257 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 Listd8e50004b3852d85b72500fd6e9ac257 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(); } } }
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 Listd8e50004b3852d85b72500fd6e9ac257 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 Example of reading and writing CSV files in Java using the OpenCSV library. For more information, please follow other related articles on the PHP Chinese website!