は、だから私のCSVファイルは、私がCSVデータの2行目からリンクリストにスキャンを開始するにはどうすればよいですか?
public class scanner {
public static void main(String[] args) throws IOException {
// open file input stream
BufferedReader reader = new BufferedReader(new FileReader(
"amis.csv"));
// read file line by line
String line = null;
Scanner scanner = null;
int index = 0;
List<amis> empList = new ArrayList<>();
while ((line = reader.readLine()) != null) {
amis emp = new amis();
scanner = new Scanner(line);
scanner.useDelimiter(",");
while (scanner.hasNext()) {
String data = scanner.next();
if (index == 0) {
emp.setId(data);
} else if (index == 4) {
emp.setPair(data);
} else if (index == 3) {
emp.setWarning(data);
} else if (index == 2) {
emp.setPeriod(data);
}else if (index == 1) {
emp.setSpeed(data);
}else {
System.out.println("invalid data::" + data);
}
index++;
}
index = 0;
empList.add(emp);
}
//close reader
reader.close();
System.out.println(empList);
}
持っているしかし、私のリンクリストの出力は
IDが表示されます私のスキャナファイルでExcel
で speed period warning pair
1 26 1 1 1
2 26 1 1 1
3 26 1 1 1
4 26 1 1 1
のようなものが含まれている= "" ::対= "ペア" ::警告= "警告" ::期間= "期間" ::スピード= "速度"、
ID = "1" ::ペア= 1 ::警告= 1 ::期間= 1 ::速度= 26,
ID = "2" ::ペア= 1 :: = 1警告::期間= 1 ::速度= 26、
ID = "3" ::ペア= 1 ::警告= 1 ::期間= 1 ::スピード= 26、
ID = "4" ::ペア= 1つの::警告= 1つの::期間= 1 ::スピード= 26、
どのように私はちょうどから開始します2行目。
'while'ループの前に' reader.readLine(); 'を呼び出します。 –
あなたは本当にそのようなcsvファイルを読む必要がありますか?使用しない場合は[commons-csv](https://commons.apache.org/proper/commons-csv/)。はいの場合は、推奨されるファイル読み取り方法を使用してください。[Javaでリソースを試してください](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) – adgon92
@AndyTurnerありがとう!それはそれを解決した – computer