Camel: How to implement CSV data screening and sorting
During the actual development process, the data in the format of CSV (comma separation) format is one of the very common tasks.In this article, we will discuss how to use the Java language to implement the screening and sorting of CSV data.
The CSV file consists of multi -line data, and the fields in each line of data are separated by comma.First of all, we need to read CSV files and resolve them into Java objects.This can be achieved by using open source libraries, such as OpenCSV or Apache Commons CSV.
The following is a sample code using the OpenCSV library to analyze the CSV file:
import com.opencsv.CSVReader;
import java.io.FileReader;
import java.io.IOException;
public class CSVParserExample {
public static void main(String[] args) {
try {
CSVReader reader = new CSVReader(new FileReader("data.csv"));
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
// Process each CSV line data
for (String field : nextLine) {
// Treat each field
System.out.print(field + " ");
}
System.out.println();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In this example, we read the CSV file by creating a `CSVReader` object and specifying the file path to be read.Then, we use the `Readnext ()` method to read CSV data row until there is no more row to read.In each line, we use the `For-Each` cycle to process each field and output it to the console.
The screening of CSV data is usually filtered out of qualified rows according to specific conditions.For example, we can screen data based on the value of a field.The following is an example code:
import com.opencsv.CSVReader;
import java.io.FileReader;
import java.io.IOException;
public class CSVFilterExample {
public static void main(String[] args) {
try {
CSVReader reader = new CSVReader(new FileReader("data.csv"));
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
// Filter condition: Data from screening the name field equal to "John"
if (nextLine[0].equals("John")) {
for (String field : nextLine) {
System.out.print(field + " ");
}
System.out.println();
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In this example, we use conditional statements in the cycle to determine whether the first field of each line is equivalent to "John". If so, the line of the line is output.
The sorting of CSV data is usually sorted by the data based on the values of a field.The following is an example code:
import com.opencsv.CSVReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
public class CSVSortExample {
public static void main(String[] args) {
try {
CSVReader reader = new CSVReader(new FileReader("data.csv"));
String[] nextLine;
String [] [] data = new string [10] []; // Assume that the data is up to 10 lines
int i = 0;
while ((nextLine = reader.readNext()) != null) {
data[i++] = nextLine;
}
reader.close();
// Sort according to the first field
Arrays.sort(data, Comparator.comparing(row -> row[0]));
// Data after output sorting
for (String[] row : data) {
if (row != null) {
for (String field : row) {
System.out.print(field + " ");
}
System.out.println();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
In this example, we first use a two -dimensional string array `Data` to store read CSV data.Then, we use the method of `complantor.comparing ()` to specify the sorting according to the first field.Finally, we use the `Arrays.sort () method to sort the data and output the results in the order of sorting.
To sum up, using Java can easily process the screening and sorting of CSV data.This article discusses how to use the OpenCSV library to analyze the CSV file and gives a sample code for screening and sorting.Developers can make appropriate modifications and expansion according to actual needs and conditions.