我尝试对CSV文件进行排序时遇到问题。 我需要从一个时间段获得10个最大频率。 比如,从10:43:12开始每次扫描并将它们写在一行中。
要写它们我没有问题,我的问题是当我尝试分开时间段并单独计算每次。
public static void writeFile(String csvFile, String newCsv) {
//Delimiter used in CSV file
String COMMA_DELIMITER = ",";
String NEW_LINE_SEPARATOR = "\n";
//CSV file header
String FILE_HEADER = "Time, ID, Lat, Lon, Alt, SSID1, MAC1, Frequncy1, Signal1,"
+ " SSID2, MAC2, Frequncy2, Signal2,"
+ " SSID3, MAC3, Frequncy3, Signal3,"
+ " SSID4, MAC4, Frequncy4, Signal4,"
+ " SSID5, MAC5, Frequncy5, Signal5,"
+ " SSID6, MAC6, Frequncy6, Signal6,"
+ " SSID7, MAC7, Frequncy7, Signal7,"
+ " SSID8, MAC8, Frequncy8, Signal8,"
+ " SSID9, MAC9, Frequncy9, Signal9,"
+ " SSID10, MAC10, Frequncy10, Signal10";
FileWriter fileWriter = null;
String line = "";
String cvsSplitBy = ",";
int t=0;
try {
fileWriter = new FileWriter(newCsv);
//Write the CSV file header
fileWriter.append(FILE_HEADER.toString());
//Add a new line separator after the header
fileWriter.append(NEW_LINE_SEPARATOR);
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
String mod=returnMod(csvFile);
br.readLine(); // this will read the first line
br.readLine();// this will read the second line
while ((line = br.readLine()) != null) {
// use comma as separator(There is 11 column)
String[] column = line.split(cvsSplitBy);
fileWriter.append(column[3]);//time
String s=column[3];
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(mod);//model
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(column[6]);//lat
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(column[7]);//lon
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(column[8]);//alt
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(column[1]);//wifi
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(column[0]);//MAC
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(column[4]);//frq
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(column[5]);//signal
fileWriter.append(NEW_LINE_SEPARATOR);
}
}
System.out.println("CSV file was created successfully !!!");
} catch (Exception e) {
System.out.println("Error in CsvFileWriter !!!");
e.printStackTrace();
} finally {
try {
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
System.out.println("Error while flushing/closing fileWriter !!!");
e.printStackTrace();
}
}
}
此代码只是将所有数据写入新文件,我试图更改它以满足我的需求,但我无法做到这一点
答案 0 :(得分:0)
我会声明一个类来保存信息:
public static class Info {
public final String time;
public final int frq;
public final String mod;
public final String lat;
public final String lon;
public final String alt;
public final String wifi;
public final String mac;
public final String signal;
public Info(String[] column, String mod) {
time = column[3];
this.mod = mod;
lat = column[6];
lon = column[7];
alt = column[8];
wifi = column[1];
mac = column[0];
frq = Integer.parseInt(column[4]);
signal = column[5];
}
}
用于存储10个最大频率信息的信息数组。循环变为:
Info[] max = new Info[10];
int count = 0;
while ((line = br.readLine()) != null) {
// use comma as separator(There is 11 column)
String[] column = line.split(cvsSplitBy);
Info info = new Info(column, mod);
if (count > 0 && !max[0].time.equals(info.time)) {
printPeriod(max, count, fileWriter);
count = 0;
}
int i = 0;
while (i < count && max[i].frq > info.frq) {
++i;
}
while (i < count) {
Info pred = max[i];
max[i] = info;
info = pred;
++i;
}
if (count < max.length) {
max[count++] = info;
}
}
if (count > 0) {
printPeriod(max, count, fileWriter);
}
这是printPeriod方法:
private static void printPeriod(Info[] max, int count, Writer fileWriter) {
fileWriter.append(max[0].time);
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(max[0].mod);
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(max[0].lat);
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(max[0].lon);
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(max[0].alt);
for (int i = 0; i < count; ++i) {
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(max[i].wifi);
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(max[i].mac);
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(Integer.toString(max[i].frq));
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(max[i].signal);
}
fileWriter.append(NEW_LINE_SEPARATOR);
}