open csv を使用してファイルを解析する方法を教えてもらえますか? 私は何日も試していますが、結果が得られませんでした。これが私のコードです
package csv;
import java.io.IOException;
import au.com.bytecode.opencsv.CSVReader;
import java.io.FileReader;
import csv.*;
import java.io.*;
public class CSVParser {
public static void main(String[] args) throws IOException,java.io.FileNotFoundException {
try{
CSVReader reader = new CSVReader(new FileReader("D:\\error.txt"));
String sLine = null;
String[] nextLine;
int count=0,flag=0;
nextLine=reader.readNext();
while( (nextLine!=null) )
{
count++;
System.out.println("count::: "+count + "and length is" +nextLine.length);
System.out.println("String:::"+nextLine[0]);
if ( (nextLine.length!= 9) ) {
flag=1;
break;
}
nextLine=reader.readNext();
}
System.out.println("No of lines:::"+count);
if(flag==1)
{
reader.close();
System.out.println("errror");
movefiles m= new movefiles();
m.move("D:\\error.txt");
}
else{
System.out.println("No error");
try{
while((nextLine=reader.readNext())!=null) {
String code = nextLine[0].toString();
String commodity = nextLine[1].toString();
float contract_price = Float.parseFloat(nextLine[2].toString());
float open_contract_vol = Float.parseFloat(nextLine[3].toString());
float open_contract_price = Float.parseFloat(nextLine[4].toString());
float unpriced_vol = Float.parseFloat(nextLine[5].toString());
float holdover_prod = Float.parseFloat(nextLine[6].toString());
String date = nextLine[7].toString();
String time = nextLine[8].toString();
System.out.println("Data From File is :::" + code +commodity +contract_price +open_contract_vol +open_contract_price +unpriced_vol +holdover_prod +date +time);
}}catch(Exception e)
{
e.printStackTrace();
System.err.println("here is error:::"+e.getMessage());
}
}
reader.close();
} catch(Exception e) {
e.printStackTrace();
System.err.println("error:::" +e.getMessage());
}
}
}
しかし、問題は、ファイルに 1 行が含まれていてもこのコードを実行すると、2 行が表示され、2 行目には何も含まれていないことです。空白が含まれている可能性があると思います。ファイルに 1 行が含まれていても 2 行表示される理由と、それを克服する方法を教えてください。一度に 1 行ずつ読む方法。plsは私を助けてください私はこれのためにcsvに不慣れです私はopencsv jarライブラリを使用しています