我讀過類似的主題,但沒有找到任何使用這種閱讀方法,所以我無法修復它。在閱讀Java中的csv時刪除第一行
如何在使用以下方法閱讀時跳過第一行? BTW,我有同第二讀取麻煩,用行之後:
File file = new File("attendance.csv");
代碼:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.Scanner;
public class ClassAttendance {
public static void main(String[] args) throws Exception {
int rowCount = 0;
int colCount = 0;
Scanner rc = new Scanner(new File("attendance.csv"));
while (rc.hasNextLine()) {
rowCount++;
rc.nextLine();
}
rc.close();
File file = new File("attendance.csv");
List<String> lines = Files.readAllLines(file.toPath(),
StandardCharsets.UTF_8);
double sum = 0;
double avg;
for(int n=1; n<3; ++n){
for (String line : lines) {
String[] array = line.split(",");
System.out.println(array[n]);
sum = sum + Double.parseDouble(array[n]);
}
avg = sum/rowCount;
System.out.print("Week ");
System.out.print(n);
System.out.print("Average = ");
System.out.print(avg);
System.out.println();
}
}
}
到目前爲止,我試圖把:
List<String> headerLine = Files.readLine(file.toPath());
上面的 「readAllLines」行但它不適用於我
問題是在「File file = new File(」attendance.csv「)後面的部分; – Win