web123456

Java reads csv file

import java.io.FileNotFoundException; import java.io.IOException; import java.nio.charset.Charset; import java.util.ArrayList; import com.csvreader.CsvReader; /* *Bail name: *Author: Adien_cui *Time: 2017-9-25 4:36:29 pm * Description: Read the csv file **/ public class ReadCsvFile { public static void readCsvFile(String filePath){ try { ArrayList<String[]> csvList = new ArrayList<String[]>(); CsvReader reader = new CsvReader(filePath,',',Charset.forName("GBK")); // reader.readHeaders(); //Skip the table header and comment out if you don't jump while(reader.readRecord()){ csvList.add(reader.getValues()); //Read by row and add the data of each row to the list collection } reader.close(); System.out.println("Number of rows read:"+csvList.size()); for(int row=0;row<();row++){ System.out.println("-----------------");//Print the data for each line System.out.print(csvList.get(row)[0]+","); System.out.print(csvList.get(row)[1]+","); System.out.print(csvList.get(row)[2]+","); System.out.println(csvList.get(row)[3]+",");//If the first column (i.e. the name column) contains lisa, print out the age of lisa if(csvList.get(row)[0].equals("lisa")){ System.out.println("Lisa's age is:"+csvList.get(row)[2]); } } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { String filePath = "F:\\"; readCsvFile(filePath); } }