public static void TempPreProcessor_Fixed(String input, String output){ 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))); String thisLine = null; //line 1 if((thisLine =reader.readLine()) != null) writer.write("AA,"+thisLine+"\n"); //line 2-4 for(int i = 0; i<3; i++){ if((thisLine =reader.readLine()) != null) writer.write(thisLine+"\n"); } //remaining lines while((thisLine=reader.readLine()) != null){ //add AB to start of line writer.write("AB,"+thisLine+"\n"); //copy with no change if((thisLine=reader.readLine()) != null) writer.write(thisLine+"\n"); } }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; }catch(Exception e){ throw new ServiceException("TempPreProcessor: Unknown errors: " + e.toString()); } } }
Thursday, February 24, 2011
Java Read/Write Fixed Length file sample
Posted by techgeek168 at 2:33 PM 0 comments
Labels: java
Java Read/Write CSV file sample
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()); } } }
Posted by techgeek168 at 12:46 PM 0 comments
Labels: java
Java Date Format
public static String parseDate(String inDate, String fmFormat, String toFormat){ SimpleDateFormat fmFormatter = new SimpleDateFormat(fmFormat); SimpleDateFormat toFormatter = new SimpleDateFormat(toFormat); if ( inDate == null ) return ""; // To avoid java.lang.NullPointerException if ( inDate.length() == 0 ) return ""; // Taking care of empty string. Date fromDate = null; try { fromDate = (Date) fmFormatter.parse(inDate); } catch (ParseException e) { e.printStackTrace(); } return toFormatter.format(fromDate); }
Posted by techgeek168 at 12:38 PM 0 comments
Labels: java
Java Switch Statement
int boolInt=1; switch (boolInt){ case 0: System.out.println("true = 0"); break; case 1: System.out.println("false = 1"); break; default: System.out.println("Invalid"); break; } // In Java SE 7 and later, you can use a String object in the switch statement's expression String boolStr = "true"; switch (boolStr.toLowerCase()){ case "true": System.out.println("0 = true"); break; case "false": System.out.println("1 = false"); break; default: System.out.println(" Not 0/1 = false"); break; }
Posted by techgeek168 at 11:30 AM 0 comments
Labels: java
Subscribe to:
Posts (Atom)