Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. import au.com.bytecode.opencsv.CSVReader;
  2.  
  3. public class UserBean {
  4. String username, password, street, town;
  5. int zip;
  6.  
  7. public String getPassword() { return password; }
  8. public String getStreet() { return street; }
  9. public String getTown() { return town; }
  10. public String getUsername() { return username; }
  11. public int getZip() { return zip; }
  12. public void setPassword(String password) { this.password = password; }
  13. public void setStreet(String street) { this.street = street; }
  14. public void setTown(String town) { this.town = town; }
  15. public void setUsername(String username) { this.username = username; }
  16. public void setZip(int zip) { this.zip = zip; }
  17. }
  18.  
  19. public class UserBean {
  20. String username, password, street, town;
  21. int zip;
  22.  
  23. public String getPassword() { return password; }
  24. public String getStreet() { return street; }
  25. public String getTown() { return town; }
  26. public String getUsername() { return username; }
  27. public int getZip() { return zip; }
  28. public void setPassword(String password) { this.password = password; }
  29. public void setStreet(String street) { this.street = street; }
  30. public void setTown(String town) { this.town = town; }
  31. public void setUsername(String username) { this.username = username; }
  32. public void setZip(int zip) { this.zip = zip; }
  33. }
  34.  
  35. username, password, date, zip, town
  36. Klaus, qwexyKiks, 17/1/2007, 1111, New York
  37. Oufu, bobilop, 10/10/2007, 4555, New York
  38.  
  39. class ReadingObjects {
  40. public static void main(String[] args) throws Exception{
  41. ICsvBeanReader inFile = new CsvBeanReader(new FileReader("foo.csv"), CsvPreference.EXCEL_PREFERENCE);
  42. try {
  43. final String[] header = inFile.getCSVHeader(true);
  44. UserBean user;
  45. while( (user = inFile.read(UserBean.class, header, processors)) != null) {
  46. System.out.println(user.getZip());
  47. }
  48. } finally {
  49. inFile.close();
  50. }
  51. }
  52. }
  53.  
  54. final CellProcessor[] processors = new CellProcessor[] {
  55. new Unique(new StrMinMax(5, 20)),
  56. new StrMinMax(8, 35),
  57. new ParseDate("dd/MM/yyyy"),
  58. new Optional(new ParseInt()),
  59. null
  60. };
  61.  
  62. + maven
  63.  
  64. + maven - release version // had some cryptic issues at _Hudson_ with snapshot references => prefer to be on a safe side
  65.  
  66. + code examples
  67.  
  68. + open source // as in "can hack myself if needed"
  69.  
  70. + understandable javadoc // as opposed to eg javadocs of _genjava gj-csv_
  71.  
  72. + compact API // YAGNI (note *flatpack* seems to have much richer API than OpenCSV)
  73.  
  74. - reference to specification used // I really like it when people can explain what they're doing
  75.  
  76. - reference to _RFC 4180_ support // would qualify as simplest form of specification to me
  77.  
  78. - 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
  79.  
  80. + bug tracking
  81.  
  82. + active // as in "can submit a bug and expect a fixed release soon"
  83.  
  84. + positive feedback // Recommended By 51 users at sourceforge (as of now)
  85.  
  86. / ************ For Reading ***************/
  87.  
  88. import java.io.FileNotFoundException;
  89. import java.io.IOException;
  90.  
  91. import com.csvreader.CsvReader;
  92.  
  93. public class CsvReaderExample {
  94.  
  95. public static void main(String[] args) {
  96. try {
  97.  
  98. CsvReader products = new CsvReader("products.csv");
  99.  
  100. products.readHeaders();
  101.  
  102. while (products.readRecord())
  103. {
  104. String productID = products.get("ProductID");
  105. String productName = products.get("ProductName");
  106. String supplierID = products.get("SupplierID");
  107. String categoryID = products.get("CategoryID");
  108. String quantityPerUnit = products.get("QuantityPerUnit");
  109. String unitPrice = products.get("UnitPrice");
  110. String unitsInStock = products.get("UnitsInStock");
  111. String unitsOnOrder = products.get("UnitsOnOrder");
  112. String reorderLevel = products.get("ReorderLevel");
  113. String discontinued = products.get("Discontinued");
  114.  
  115. // perform program logic here
  116. System.out.println(productID + ":" + productName);
  117. }
  118.  
  119. products.close();
  120.  
  121. } catch (FileNotFoundException e) {
  122. e.printStackTrace();
  123. } catch (IOException e) {
  124. e.printStackTrace();
  125. }
  126.  
  127. }
  128.  
  129. }
  130.  
  131. /************* For Writing ***************************/
  132.  
  133. import java.io.File;
  134. import java.io.FileWriter;
  135. import java.io.IOException;
  136.  
  137. import com.csvreader.CsvWriter;
  138.  
  139. public class CsvWriterAppendExample {
  140.  
  141. public static void main(String[] args) {
  142.  
  143. String outputFile = "users.csv";
  144.  
  145. // before we open the file check to see if it already exists
  146. boolean alreadyExists = new File(outputFile).exists();
  147.  
  148. try {
  149. // use FileWriter constructor that specifies open for appending
  150. CsvWriter csvOutput = new CsvWriter(new FileWriter(outputFile, true), ',');
  151.  
  152. // if the file didn't already exist then we need to write out the header line
  153. if (!alreadyExists)
  154. {
  155. csvOutput.write("id");
  156. csvOutput.write("name");
  157. csvOutput.endRecord();
  158. }
  159. // else assume that the file already has the correct header line
  160.  
  161. // write out a few records
  162. csvOutput.write("1");
  163. csvOutput.write("Bruce");
  164. csvOutput.endRecord();
  165.  
  166. csvOutput.write("2");
  167. csvOutput.write("John");
  168. csvOutput.endRecord();
  169.  
  170. csvOutput.close();
  171. } catch (IOException e) {
  172. e.printStackTrace();
  173. }
  174.  
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement