Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import au.com.bytecode.opencsv.CSVReader;
- public class UserBean {
- String username, password, street, town;
- int zip;
- public String getPassword() { return password; }
- public String getStreet() { return street; }
- public String getTown() { return town; }
- public String getUsername() { return username; }
- public int getZip() { return zip; }
- public void setPassword(String password) { this.password = password; }
- public void setStreet(String street) { this.street = street; }
- public void setTown(String town) { this.town = town; }
- public void setUsername(String username) { this.username = username; }
- public void setZip(int zip) { this.zip = zip; }
- }
- public class UserBean {
- String username, password, street, town;
- int zip;
- public String getPassword() { return password; }
- public String getStreet() { return street; }
- public String getTown() { return town; }
- public String getUsername() { return username; }
- public int getZip() { return zip; }
- public void setPassword(String password) { this.password = password; }
- public void setStreet(String street) { this.street = street; }
- public void setTown(String town) { this.town = town; }
- public void setUsername(String username) { this.username = username; }
- public void setZip(int zip) { this.zip = zip; }
- }
- username, password, date, zip, town
- Klaus, qwexyKiks, 17/1/2007, 1111, New York
- Oufu, bobilop, 10/10/2007, 4555, New York
- class ReadingObjects {
- public static void main(String[] args) throws Exception{
- ICsvBeanReader inFile = new CsvBeanReader(new FileReader("foo.csv"), CsvPreference.EXCEL_PREFERENCE);
- try {
- final String[] header = inFile.getCSVHeader(true);
- UserBean user;
- while( (user = inFile.read(UserBean.class, header, processors)) != null) {
- System.out.println(user.getZip());
- }
- } finally {
- inFile.close();
- }
- }
- }
- final CellProcessor[] processors = new CellProcessor[] {
- new Unique(new StrMinMax(5, 20)),
- new StrMinMax(8, 35),
- new ParseDate("dd/MM/yyyy"),
- new Optional(new ParseInt()),
- null
- };
- + maven
- + maven - release version // had some cryptic issues at _Hudson_ with snapshot references => prefer to be on a safe side
- + code examples
- + open source // as in "can hack myself if needed"
- + understandable javadoc // as opposed to eg javadocs of _genjava gj-csv_
- + compact API // YAGNI (note *flatpack* seems to have much richer API than OpenCSV)
- - reference to specification used // I really like it when people can explain what they're doing
- - reference to _RFC 4180_ support // would qualify as simplest form of specification to me
- - releases changelog // absence is quite a pity, given how simple it'd be to get with maven-changes-plugin // _flatpack_, for comparison, has quite helpful changelog
- + bug tracking
- + active // as in "can submit a bug and expect a fixed release soon"
- + positive feedback // Recommended By 51 users at sourceforge (as of now)
- / ************ For Reading ***************/
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import com.csvreader.CsvReader;
- public class CsvReaderExample {
- public static void main(String[] args) {
- try {
- CsvReader products = new CsvReader("products.csv");
- products.readHeaders();
- while (products.readRecord())
- {
- String productID = products.get("ProductID");
- String productName = products.get("ProductName");
- String supplierID = products.get("SupplierID");
- String categoryID = products.get("CategoryID");
- String quantityPerUnit = products.get("QuantityPerUnit");
- String unitPrice = products.get("UnitPrice");
- String unitsInStock = products.get("UnitsInStock");
- String unitsOnOrder = products.get("UnitsOnOrder");
- String reorderLevel = products.get("ReorderLevel");
- String discontinued = products.get("Discontinued");
- // perform program logic here
- System.out.println(productID + ":" + productName);
- }
- products.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /************* For Writing ***************************/
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import com.csvreader.CsvWriter;
- public class CsvWriterAppendExample {
- public static void main(String[] args) {
- String outputFile = "users.csv";
- // before we open the file check to see if it already exists
- boolean alreadyExists = new File(outputFile).exists();
- try {
- // use FileWriter constructor that specifies open for appending
- CsvWriter csvOutput = new CsvWriter(new FileWriter(outputFile, true), ',');
- // if the file didn't already exist then we need to write out the header line
- if (!alreadyExists)
- {
- csvOutput.write("id");
- csvOutput.write("name");
- csvOutput.endRecord();
- }
- // else assume that the file already has the correct header line
- // write out a few records
- csvOutput.write("1");
- csvOutput.write("Bruce");
- csvOutput.endRecord();
- csvOutput.write("2");
- csvOutput.write("John");
- csvOutput.endRecord();
- csvOutput.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement