我正在编写一个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);
}
}
暂无答案!
目前还没有任何答案,快来回答吧!