Read CSV file column by column

前端 未结 8 1696
萌比男神i
萌比男神i 2020-11-29 04:31

I want to read specific columns from a multi column csv file and print those columns in other csv file using Java. Any help please? Following is my code to print each token

8条回答
  •  一向
    一向 (楼主)
    2020-11-29 05:20

    You should use the excellent OpenCSV for reading and writing CSV files. To adapt your example to use the library it would look like this:

    public class ParseCSV {
      public static void main(String[] args) {
        try {
          //csv file containing data
          String strFile = "C:/Users/rsaluja/CMS_Evaluation/Drupal_12_08_27.csv";
          CSVReader reader = new CSVReader(new FileReader(strFile));
          String [] nextLine;
          int lineNumber = 0;
          while ((nextLine = reader.readNext()) != null) {
            lineNumber++;
            System.out.println("Line # " + lineNumber);
    
            // nextLine[] is an array of values from the line
            System.out.println(nextLine[4] + "etc...");
          }
        }
      }
    }
    

提交回复
热议问题