Tuesday, January 25, 2011

CSV Data Loader

Here I am sharing quick CSV Data Loader using scanner.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class DataLoader {

public void laodData(String fileName) {
Scanner scanner = null;
try {
scanner = new Scanner(new File(fileName));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println("CSV Line : " + line);
Scanner scanner2 = new Scanner(line);
scanner2.useDelimiter(", *");

// Read and sum numbers.
while (scanner2.hasNext()) {
System.out.println(scanner2.next());
}
System.out.println("________________ LINE END ____________________");

}
}

public static void main(String[] args) {
new DataLoader().laodData("c:\\test3.csv");
}
}