Sample Java program that parses a CSV file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | package com.as400samplecode;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.List;import java.util.StringTokenizer;import au.com.bytecode.opencsv.CSVReader;import au.com.bytecode.opencsv.bean.ColumnPositionMappingStrategy;import au.com.bytecode.opencsv.bean.CsvToBean;public class ParseCSVFile { public static void main(String[] args) { String filename = args[0]; ParseCSVFile parseCSVFile = new ParseCSVFile(); System.out.println("Starting to parse CSV file using StringTokenizer"); parseCSVFile.parseUsingStringTokenizer(filename); System.out.println("Starting to parse CSV file using opencsv"); parseCSVFile.parseUsingOpenCSV(filename); System.out.println("Starting to parse CSV file and map to Java Bean"); parseCSVFile.parseCSVtoBean(filename); } private void parseUsingStringTokenizer(String filename){ try { //create BufferedReader to read CSV file BufferedReader br = new BufferedReader( new FileReader(filename)); String record = ""; StringTokenizer st = null; int rowNumber = 0; int cellIndex = 0; //read comma separated file line by line while( (record = br.readLine()) != null) { rowNumber++; //break comma separated line using "," st = new StringTokenizer(record, ","); while(st.hasMoreTokens()) { //display CSV values cellIndex++; System.out.println("Cell column index: " + cellIndex); System.out.println("Cell Value: " + st.nextToken()); System.out.println("---"); } //reset cell Index number cellIndex = 0; } } catch(Exception e){ System.out.println("Exception while reading csv file: " + e); } } private void parseUsingOpenCSV(String filename){ try { CSVReader reader = new CSVReader(new FileReader(filename)); String [] nextLine; int rowNumber = 0; while ((nextLine = reader.readNext()) != null) { rowNumber++; for(int i = 0;i< nextLine.length; i++){ //display CSV values System.out.println("Cell column index: " + i); System.out.println("Cell Value: " + nextLine[i]); System.out.println("---"); } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void parseCSVtoBean(String filename){ try { CSVReader reader = new CSVReader(new FileReader(filename), ',', '\"', 2); ColumnPositionMappingStrategy<OrderDetail> mappingStrategy = new ColumnPositionMappingStrategy<OrderDetail>(); mappingStrategy.setType(OrderDetail.class); // the fields to bind do in your JavaBean String[] columns = new String[] {"itemNumber", "description", "price","quantity"}; mappingStrategy.setColumnMapping(columns); CsvToBean<OrderDetail> csv = new CsvToBean<OrderDetail>(); List<OrderDetail> orderList = csv.parse(mappingStrategy, reader); for(int i = 0;i< orderList.size(); i++){ OrderDetail orderDetail = orderList.get(i); //display CSV values System.out.println("Item Number: " + orderDetail.getItemNumber()); System.out.println("Description: " + orderDetail.getDescription()); System.out.println("Price: " + orderDetail.getPrice()); System.out.println("Quantity: " + orderDetail.getQuantity()); System.out.println("---"); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }} |
Java Bean - OrderDetail.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.as400samplecode;public class OrderDetail { String itemNumber = null; String description = null; Double price = 0.0; int quantity = 0; public String getItemNumber() { return itemNumber; } public void setItemNumber(String itemNumber) { this.itemNumber = itemNumber; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; }} |


No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.