Read CSV File, sort records, and output a new csv file
Hi, I am new to java so please bear with me. I have a csv file that contains an order details. So there are multiple order numbers repeated for each item on the order.
What I want to do is create a String with the order number listed once and all the items that go with the order listed after the order number on one line.
What I have done so far is read the csv file that contains the records and store them in a hashtable. Then I tried to sort them from there. I am not sure if this is the best approach or not. Again I am an extreme newbie. sorry.
Here is the code I have to get the records and store them and then write them back out to a new csv file. Any help would be greatly appreciated.
Connection conn = DriverManager.getConnection("jdbc:relique:csv:" + directory );
Statement stmt = conn.createStatement();
// Select the ID and NAME columns from sample.csv
String sqlString = "SELECT order,item FROM Items";
ResultSet rs = stmt.executeQuery(sqlString);
Hashtable ht = new Hashtable();
FileWriter file = new FileWriter("C:\\newFile.csv");
BufferedWriter out = new BufferedWriter(file);
StringBuilder sb = new StringBuilder();
String key = rs.getString("order");
String value = rs.getString("item").trim();
String header = "Order" + "," + "Item" + "\n";
sb.append(key + "," + value + "\n" );