Java:如何解析大量数据。csv文件?
我正在编写一个Java程序,可以读取。csv文件,并从文件的特定列中提取信息。csv文件,并将它们全部放入一个单独的文件中。csv文件。我现在的工作方式很好,但是我有100多个。我需要从csv文件中提取,所以像我在main方法中那样一个接一个地写出它们似乎效率不高。我不太熟悉数据结构,但有没有一种方法可以实现ArrayList,存储我所有的数据。csv文件,并获得相同的结果,而不必太多地修改我的代码
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.*;
public class csvParser {
public static FileWriter createOutputCsvFile(String pathToFile) {
try {
FileWriter csvWriter = new FileWriter(pathToFile);
return csvWriter;
}
catch (Exception e) {
System.err.println("Error: " + e.getMessage());
return null;
}
}
// Parses and calculates the source count, destination count and incoming/outgoing packet sizes
public static void parseCsvFile(String pathToFile, FileWriter csvWriter) {
try {
String row;
int incomingPacketSize = 0;
int outgoingPacketSize = 0;
int sourceCount = 0;
int destinationCount = 0;
BufferedReader csvReader = new BufferedReader(new FileReader(pathToFile));
while ((row = csvReader.readLine()) != null) {
String[] line = row.split(",");
String source = line[2];
String destination = line[3];
// remove quotes around the length string
String length = line[5].replace("\"", "");
if (source.equals("\"192.168.100.180\"")) {
sourceCount++;
incomingPacketSize += Integer.parseInt(length);
}
if (destination.equals("\"192.168.100.180\"")) {
destinationCount++;
outgoingPacketSize += Integer.parseInt(length);
}
}
// write to output csv
writeOutputToCSV(csvWriter, sourceCount, destinationCount, incomingPacketSize, outgoingPacketSize);
}
catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
// Write the source count, destination count and incoming/outgoing packet sizes to the output csv file
public static void writeOutputToCSV(FileWriter csvWriter, int sourceCount, int destinationCount, int incomingPacketSize, int outgoingPacketSize) {
try {
csvWriter.append(sourceCount + "," + destinationCount + ","+ incomingPacketSize + "," + outgoingPacketSize + "\n");
csvWriter.flush();
}
catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
public static void main(String[] args) {
FileWriter csvWriter = createOutputCsvFile("/Users/saadkhan/Desktop/Assignment/csv/Outpit.csv");
if (csvWriter == null) {
System.out.println("Error creating output csv file");
System.exit(0);
}
parseCsvFile("/Users/saadkhan/Desktop/Assignment/csv/1DNSSplit1.csv", csvWriter);
parseCsvFile("/Users/saadkhan/Desktop/Assignment/csv/1DNSSplit2.csv", csvWriter);
}
}
共 (0) 个答案