The two text files ,input file and template are read from the disk , Hash Maps / Array Lists are being extensively used to check and arrange for the order and written in an output file.Basic Advantage is without altering the code and by changing the template file, display order can be altered in the output file.This has been successfully implemented in our project and could be useful wherever text file needs to be ordered based on particular order.
import java.io.*; import java.util.*; import java.util.ArrayList; class sortFile { public static void main (String [] args) throws IOException { String inputFile=""; String sortFile=""; //check for Arguments if (args.length < 2) { System.out.println("Please Enter Input and Sort File Name"); System.exit(0); } else { inputFile= args[0]; sortFile = args[1]; System.out.println("Input File Name " + inputFile); System.out.println("Sort File Name " + sortFile); } //File To be read and sorted FileReader f = new FileReader("c:/sort/"+inputFile+".txt"); BufferedReader br =new BufferedReader(f); String s=""; HashMap hmInput = new HashMap(); String key = ""; String componenet =""; String compValue= ""; int compValueint = 0; //input file in a Hash Map while ((s=br.readLine())!=null) { StringTokenizer st =new StringTokenizer(s,"|"); while (st.hasMoreTokens()) { key=st.nextToken().trim(); compValue=st.nextToken().trim(); compValueint = Integer.parseInt(compValue); hmInput.put(key, new Integer(compValueint)); } } br.close(); f.close(); // Sort File Template FileReader fSort = new FileReader("c:/sort/"+sortFile+".txt"); BufferedReader brSort =new BufferedReader(fSort); String sort=""; ArrayList str = new ArrayList(); //Adding the sort file template to Array list,presering the order. while ((sort=brSort.readLine())!=null) { str.add(sort); } brSort.close(); fSort.close(); //String str[] = {"WELCOME_OLB","PRIMARY_MEDIA_ADDR_CHG","ACCT_DECISION_NOTIFY","OLB_TEST_MAIL","OLB_PASSCD_RESET","OLB_NEW_P2P_ACCT","OLB_ID_LOOK_UP","OLB_ID_LOCK_OUT","OLB_ID_CHG","OLB_EMAIL_CHG_OLD","OLB_CREDIT_CARD_OUTSIDE_US","OLB_CHECK_CARD_OUTSIDE_US","OLB_BAD_ACT_DEBIT","OLB_BAD_ACT_CREDIT","MB_ENROLLMENT","INTERNAL_MAIL_BOX_NOTIFICATION","DE_ENROLLMENT","CREDIT_CARD_PYMT_POSTED","CREDIT_CARD_LIMIT_THRESHOLD","OLB_CREDIT_CARD_RETURNS","OLB_CREDIT_CARD_BAL_EXCEED","OLB_CREDIT_CARD_BAL_DROP","OLB_CHECK_REORDER","OLB_STOP_ALERT","OLB_THRESHOLD_CREDIT","OLB_THRESHOLD_CHECK_DEBIT","OLB_THRESHOLD_ATM_CARD_DEBIT","OLB_THRESHOLD_ACH_DEBIT","OLB_THRSHOLD_BILLPAY_DEBIT","OLB_THRESHOLD_ELECTRONIC_DEBIT","ACCT_EDOC_REMIND","ACCT_SAVED_APPL","ACCT_LOW_BAL_THRESHOLD","ACCT_SUBMIT_CONFIRM","ACCT_SIGN_CARD_REMIND2","ACCT_SIGN_CARD_REMIND1","ACCT_SETUP_SAVED_APPL_REMIND","ACCT_SETUP_SAVED_APPL","ACCT_SAVED_COUNTER_REMIND","ACCT_SAVED_COUNTER_OFFER","ACCT_SAVED_APPL_REMIND","OLB_CHECK_POSTED","OLB_DEPOSIT_AVAL_BAL","OLB_CREDIT_CARD_NOT_PRESENT","CREDIT_CARD_PYMT_DUE","ACH_CREDITS","ACCT_ZERO_FUNDING_REMIND","OLB_ADDR_PHONE_CHG","ODP","NSF","OLB_CASH_ADV_CREDIT","OLB_CHECK_CARD_NOT_PRESENT","OLB_CONV_CHECK_ORDER"}; String tempVar=""; int compVal=0; // writing the output FileWriter fw = new FileWriter("c:/sort/output.txt"); BufferedWriter brw = new BufferedWriter(fw); int tempVarsize=0; for (int i=0; i <str.size(); i++) { tempVar=(String)str.get(i); brw.write(tempVar+ ":"); tempVarsize = tempVar.length(); //arranging the file for 50 characters between columns while( tempVarsize < 50) { brw.write("-"); tempVarsize = tempVarsize+1; } if(hmInput.get(tempVar)!=null) { compVal=((Integer)hmInput.get(tempVar)).intValue(); brw.write(compVal+"n"); hmInput.remove(tempVar); } else { brw.write("No Value"); brw.write("n"); } } brw.close(); fw.close(); } }