java讀取記憶體中的csv檔案,跳過第一行
阿新 • • 發佈:2019-02-12
package ApacheCommonCSV;
import junit.framework.TestCase;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
public class AppTest extends TestCase
{
public void testApp(){
try {
FileReader filereader = new FileReader("/Users/student/Desktop/file.csv");
BufferedReader bufferedReader = new BufferedReader(filereader);
bufferedReader.readLine();// try-catch omitted
CSVFormat format = CSVFormat.DEFAULT.withDelimiter(',' );
CSVParser parser = new CSVParser(bufferedReader, format);
List<CSVRecord> records = parser.getRecords();//跳過第一行所有行的記錄
for(int i=0;i<records.size();i++){
double[] temp=new double[records.get(i).size()];
for(int j=0;j<records.get(i).size();j++){
temp[j]=Double.parseDouble(records.get(i).get(j));
//System.out.print(temp[j]+" ");
}
for(int ii=0;ii<temp.length;ii++){
System.out.print(temp[ii]+" ");
}
System.out.println();
}
}catch (Exception e){
System.out.print("please check your upload");
}
}
}
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.5</version>
</dependency>