Create ArrayList by reading flat file

84 Views Asked by At

I am attempting to read the flat file and transfer its contents to ArrayList.By the code is not working

flat file

BEG
SN:M7254168
VER:1.10
HC 00002    4077 215
D   4080006441610001
D   8475190354020001
END


public class BigFlatFileProcess {
    private static Logger logger = Logger.getLogger(BigFlatFileProcess.class);

    public ArrayList<Item> process(String sile) {

        ArrayList<Item> huge = new ArrayList<Item>();

        ArrayList<Integer> nlist = getLineNo(sile);

        try{
        for(int i=0; i<=nlist.size();i++){

            System.out.println(nlist.get(i)+" "+nlist.get(i+1));
             logger.info("File contents are ");
             ArrayList<Item>    pcom = showLines(sile, nlist.get(i)-1,nlist.get(i+1)-2);
             System.out.println(" Number of items in this bin "+pcom.size());
             for(int j=0;j<pcom.size();j++){
                 logger.info("from bigflatfileprocess"+pcom.get(j).toString());
                  huge.add(pcom.get(j));
             }
            System.out.println("STARTING ..");

        }
        }catch(Exception e){
            logger.info(" Exception occured in file processing that can be safely ignored");

        }


        logger.info(" Processed the AML flat file successfully  "); 
        return huge;
    }

     public ArrayList<Item> showLines( String fileName, int startLine, int endLine){
         ArrayList<Item>   preList = new ArrayList<Item>();
         int currentLineNo = 0;
         String line = null;
         try(BufferedReader bReader = new BufferedReader(new FileReader(fileName)) ){
             Item pre = new Item(); 
             while(currentLineNo<startLine){
                 if(bReader.readLine()==null){
                     throw new IOException("File too small");
                 }

                 currentLineNo++;
             }

             for(  ;currentLineNo <=endLine;currentLineNo++ ){
                 line = bReader.readLine();
                 if(line==null){
                     return null;
                 }

                 if(line.startsWith("H")){
                     String bnum = line.substring(17,20);
                     pre.setBin(bnum);
                     String cnum = line.substring(12,16);
                     pre.setCycNumber(cnum);

                 }
                 if(line.startsWith("D")){
                     String upcNum = line.substring(4, 16);
                     pre.setUpcCode(upcNum);
                     String qty = line.substring(16,20);
                     pre.setQuantity(Integer.parseInt(qty));
                     logger.info(line); 


                     logger.info(" The List Bin :"+pre.getBin()+" upcCode: "+pre.getUpcCode()+"  Quantity: "+pre.getQuantity()+" cycle = "+pre.getCycNumber());

                 }

                 preList.add(pre);
             }


         } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } 
         return preList;
     }

     public  ArrayList<Integer> getLineNo(String fileName){

         ArrayList<Integer> hlist = new ArrayList<Integer>();

         try(LineNumberReader lReader = new LineNumberReader(new FileReader(fileName)) ){

             String line;

             while((line = lReader.readLine()) != null){
                 if(line.startsWith("H")||line.startsWith("END")){
                     int i = lReader.getLineNumber();
                     logger.info("Line numbers "+i);
                     hlist.add(i);
                 }


             }


         } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

         return hlist;

         }

     }

The log is shown in this picture log:contents of flatfile to arraylist I cant understand why my for loop is adding the last record to array list. Any suggestions will be deeply appreciated

1

There are 1 best solutions below

1
On

From my observation, ur for loop is missing an int variable declaration to define the starting point for the iteration. Second, ur condition states less than or equal to. U should replacing that with a less than < symbol alone. That leave out the last variable. Hope this helps.