Hi Bhaskar,
It has taken a lot of time to answer this query as it involved lot of trial and error. Try this code. I have assumed that last line of original file has no newline at the end. In case it has just initialise value of count variable to -1 instead of zero.
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Calendar; import java.util.Date; import com.sap.aii.mapping.api.AbstractTransformation; import com.sap.aii.mapping.api.StreamTransformationException; import com.sap.aii.mapping.api.TransformationInput; import com.sap.aii.mapping.api.TransformationOutput; public class RemoveExtraElement extends AbstractTransformation{ /** * @param args * @throws FileNotFoundException * @throws StreamTransformationException */ @Override public void transform(TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException { // TODO Auto-generated method stub execute(arg0.getInputPayload().getInputStream(),arg1.getOutputPayload().getOutputStream()); } public void execute(InputStream in, OutputStream out) throws StreamTransformationException { try { StringBuilder s = new StringBuilder(); byte[] b=new byte[in.available()]; if(in.read(b) >= 0) { s = new StringBuilder(s.toString()+new String(b)); } String header="\"NOTACTIVE\",\"STUD_ID\",\"FNAME\",\"LNAME\",\"MI\",\"GENDER\",\"JP_ID\",\"JP_DESC\",\"JOB_TITLE\",\"ORG_ID\"!##!\n"; s= new StringBuilder(s.insert(0,header)); java.util.Date date= new Date(); Calendar cal = Calendar.getInstance(); cal.setTime(date); int month = cal.get(Calendar.MONTH)+1; int year=cal.get(Calendar.YEAR); int day=cal.get(Calendar.DAY_OF_MONTH); cal.set(Calendar.YEAR,year); cal.set(Calendar.MONTH,month-1); cal.set(Calendar.DATE,day); String runDate1=""+year+"/"; if(month<10) { runDate1=runDate1+"0"; } runDate1=runDate1+month+"/"; String runDate2=runDate1+cal.getActualMaximum(Calendar.DAY_OF_MONTH); runDate1+="01"; int len=s.length(); int count=0; for(int i=0;i<len;++i) { if(s.charAt(i)=='\n') { count++; } } String footer="\nTB TA TC "+runDate1+" "+runDate2+" "+count; s=s.append(footer); System.out.println(s); out.write(s.toString().getBytes("UTF-8")); in.close(); out.close(); } catch(Exception e) { e.printStackTrace(); throw new StreamTransformationException(e.getMessage()); } } }
Regards
Anupam