public static void TempPreProcessor_CSV(String input, String output){ //IDataSharedCursor idc = pipeline.getSharedCursor(); BufferedReader reader = null; BufferedWriter writer = null; try { String inputfilename = input; String outputfilename = output; reader = new BufferedReader( new InputStreamReader( new FileInputStream(inputfilename))); writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(outputfilename))); int lineNum = 1; String checkNum = ""; String thisLine = ""; // comma, which is not enclosed in quotes String delims = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"; while((thisLine=reader.readLine()) != null){// till end of file StringBuffer sb = new StringBuffer(); if(thisLine.trim().equals("")) continue; // ignore blank lines if (lineNum++ == 1){ sb.append(thisLine + "\n"); writer.write(sb.toString()); continue; } String[] thisTokens = thisLine.split(delims); if (!checkNum.equalsIgnoreCase(thisTokens[0].toString())){ checkNum = thisTokens[0].toString(); sb.append("AA," + thisLine); }else{ sb.append("AB," + thisLine); } sb.append("\n"); writer.write(sb.toString()); } }catch(Exception e){ //throw new ServiceException( // "TempPreProcessor: Unknown errors: " + e.toString()); }finally{ try { if(writer != null) writer.flush(); if(reader != null) reader.close(); if(writer != null) writer.close(); reader=null; writer=null; //idc.destroy(); }catch(Exception e){ //throw new ServiceException( // "TempPreProcessor: Unknown errors: " + e.toString()); } } }
Thursday, February 24, 2011
Java Read/Write CSV file sample
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment