previous | start | next

Read CSV File

import java.util.Scanner;

public class ReadCSV
{
  public static void main(String[] args)
  {
    Scanner in = null;

    try {
       in = new Scanner(new File(args[0]));
    } catch(FileNotFoundException e) {
       System.out.printf("Unable to open input file %s\n", args[0]);
       System.exit(0);
    }
  
    String[] fields;
    String line;
    while( in.hasNextLine() ) {
      line = in.nextLine();
      fields = line.split("\\s*,\\s*");
      for(int i = 0; i < fields.length; i++) {
        System.out.printf("field %d: %s\n", i, fields[i]);
      }
    }
  }
}


previous | start | next