Link to home
Start Free TrialLog in
Avatar of g46905
g46905

asked on

Selecting specific files from the directory by checking the start value fo the file

Hello Experts,
       Once I open the dir, I  check to see if there are any files? if yes, I need to open each and every single file to see if it starts with "ISA". For example , I am looking for files which has data in this format..

ISA*00*          *00*          *01*123456789      *01*34563456INTP  *040624*2120*^*00403*000008218*0*P*:
GS*FA*123456789SQOP*NOSQO34563456*20040624*2120*1382*X*004030
ST*997*0001
AK1*CU*1401
AK2*873*0001
AK5*A
AK9*A*1*1*1
SE*6*0001
GE*1*1382
IEA*1*000008218

If it starts with ISA, I need to parse through the entire file which is normally big and store all the lines which start with "ST" in a vector so that I can apply my logic to the data in that vector.

Can you please help me how to tackle this problem.

Thanks,

ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
List list = new Vector();
File dir = new File(startDir);
File[] files = dir.listFiles();
// loop through and read
BufferedReader in = new BufferedReader(new FileReader(files[i]));
String buffer = in.readLine();
if (buffer != null && buffer.startsWith("ISA")) {
    // read the rest
    while ((buffer = in.readLine()) != null) {
        if (buffer.startsWith("ST")) {
            list.add(buffer);
        }

    }

}

in.close();
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of g46905
g46905

ASKER

Hello Expert,
               I am trying to unzip the all the zip files in our archive (  for example archives/abcd.zip files)
Once,I open the zip file, there will be number of files with different types of data in the zip file.I am concerned only with the files in which the data starts with ISA.Please have a look at the file structure mentioned below.If the data in the file starts with ISA, I need to parse the entire file and store all the lines which start with ST and SLN in a vector so that I can record the statistics in our monthly report. Can some please help me by giving some guidelines and help me achieve this task. I am new to java( 2 - 3 months). Thanks for your time

ISA*00*          *00*          *01*987654321      *14*123456789INTP  *040213*1012*U*00200*402131012*0*P*:
GS*PO*987654321*NNOMS987654321*040213*1012*402131012*X*003040
ST*850*402131012
BEG*00*G1*X0402131012**040213
DTM*102*****DT*200402131012
N1*SJ**1*123456789
N1*78**1*987654321
PO1*000001*****CR*105341*MN*P
DTM*007*****D8*20040214
DTM*197*****D8*20040229
SLN*000001*10043612001*I*10000*BZ
SI*AP*QT*R*TT*01*PG*65395705900101*R2*1*UK*T2069F*R3*1*DK*ABCDE
N1*M2**29*4669
N1*MQ**29*3811
N1*US**1*333333333
N1*DW**1*222222222
ST*850*402131012
PO1*000002*****CR*110014*MN*P
DTM*007*****D8*20040214
DTM*197*****D8*20040229
SLN*000001*10043612002*I*10000*BZ
SI*AP*QT*R*TT*01*PG*64148705900101*R2*1*UK*X00018*R3*1*DK*ABCDE
N1*M2**29*4669
N1*MQ**29*3811
N1*US**1*111111111
N1*DW**1*987654321



             
You just need to do what i've said above, only with a ZipFile. Iterate the entries and get the input stream and read it

http://javaalmanac.com/egs/java.util.zip/GetZip.html
Avatar of g46905

ASKER

I am having an issue while opening the file.

here is the method in my class which opens the zip file.I am passing the file inside the .zip file to a constructor of class EdiX12. I am getting System path not found because the file is not being recognised in the EdiX12 class.Can you please suggest me what I am supposed to do?

***************************************************************************
public static void unzipAndGetEntries(String zipfilename, Date fromDD, Date toDate) throws Exception{
            
       System.out.println(" Zip File Name :"+zipfilename);
       // zipfilename =   C:/Reporting/archives/Nicor_535537.zip/inbound/processed/X12.bpp
                                          
            try {
                  
                  Date fromDDD = fromDD;
                  Date toDDD = toDate;
                  InputStream f = null;
                  ZipFile myzipfile = new ZipFile(zipfilename);
                  for (Enumeration e = myzipfile.entries(); e.hasMoreElements();){
                     ZipEntry ze = ((ZipEntry)e.nextElement());
                     String s =  ze.getName();
               
            if (((ze.getName().indexOf(rco.getDirection())!= -1)) ) {
                             
                  File file = new File(ze.getName());
        EdiX12 hdrInf = new EdiX12(file); // I am passing the file here              
                          
            } // end of if (check file extension)
          
       }      //end of for (reading zip files)
      
            
 } catch (IOException e){ System.out.println("Error while unzipping the files : " );
       e.printStackTrace();}
            }

********************************************************************************
public EdiX12(java.io.File ifile) throws Exception {
      super();

      this.file = ifile;
            
      byte content[] = new byte[HEADER_LENGTH];
      InputStream in = null;
      int recv;
      try {
            in = new BufferedInputStream(new FileInputStream(file));
            recv = in.read(content);
      } finally {
            if (in != null)
                  in.close();
      }

      if (recv < HEADER_LENGTH) {
            throw new Exception("First line is less then " + HEADER_LENGTH + " characters");
      }
 
      String s = new String(content, 0, HEADER_LENGTH);
      
      System.out.println("String Value :" + s);
                  
      if ("ISA".equals(s.substring(0,3))) {
      x12File = true;
      senderId = s.substring(35, 50).trim();
      receiverId = s.substring(54, 69).trim();
      senderQualifier = s.substring(32, 34).trim();
      receiverQualifier = s.substring(51, 53).trim();
      number = s.substring(90, 99).trim();

      segmentDelimiter = content[HEADER_LENGTH - 1];
      elementDelimiter = content[3];
      
      }
      
}

***********************************************************************************

Avatar of g46905

ASKER

This is the error I am getting when I try to read the file in the EdiX12 class after unzipping it.


java.io.FileNotFoundException: inbound\processed\inbound/processed/X12.bpp
(The system cannot find the path specified)

This is the actual file name "C:/Reporting/archives/Nicor_535537.zip/inbound/processed/X12.bpp"
>java.io.FileNotFoundException: inbound\processed\inbound/processed/X12.bpp
>(The system cannot find the path specified)

Where do you have this file? Also check the syntax of the file separators, you have them mixed up.
Avatar of g46905

ASKER

It is in the C: drive under Reporting/archives/Nicor_535537.zip.Once we open the zip file, it has got the file structure as
"/inbound/processed/" plus the file that I am trying to read (For Example : X12.bpp).I am confused at this moment. Can you please help me?

Thanks
Can you tell us the line this erro roccurs?
Avatar of g46905

ASKER

This is in the EdiX12 constructor I mentioned above

try {
          in = new BufferedInputStream(new FileInputStream(file));// <==========================
          recv = in.read(content);
     } finally {
          if (in != null)
               in.close();

Please let me know if I need to change my logic.

Thanks
For some reason it can't find the file. What happens if you do:

ifile.exists();

Does it return true or false?
There is some confusion here:

>>File file = new File(ze.getName());

means that you are attempting to pass a file *possibly in the current directory or a subdirectory thereof* (depending on the entry name) to the ctor. The only connection with the zip file is that the entry (which is anyway not a file incidentally) name String  is being taken out of it and used to construct a path (likely a wrong one) in the filesystem. Is that your intention?

Avatar of g46905

ASKER

This is what I was trying to do. i am reading the zip file name(C:/Reporting/archives/Nicor_535537.zip) from the command line. Once you open the Nicor_535537.zip file, we have the following file structure
"/inbound/processed/filename"

in the method unzipAndGetEntries(String zipfilename, Date fromDD, Date toDate) above,I am unzipping the file to see if there are nay files in the .zip file. If yes, I want to pass that file to the ctor of the EdiX12 class so that I can read the valid file (strats with "ISA") .This is what I intended to do. Please let me know if I am going wrong somewhere.

Thanks,
g46905
>>we have the following file structure "/inbound/processed/filename"

OK. At best, that will try to open the file C:\inbound\processed\filename. Is that what you want?
Avatar of g46905

ASKER

No. I want it to open "C:/Reporting/archives/Nicor_535537.zip/inbound/processed/filename". Can you please direct me in the right direction by giving your valuable suggestion.
Then you'll have to prepend the drive and path to that entry before passing to the ctor
Avatar of g46905

ASKER

I have done that too. But still doesnt work. I was wondering if I have to do something with the slashes in the path.Please suggest.

Thanks,
>>Nicor_535537.zip

Is that really a directory?
Avatar of g46905

ASKER

That is one of the zip file in our archives.Within the zip file, we have /inbound/processed/filename. I really appreciate all your patience in this issue.

Thanks again,
Then you can't open

C:/Reporting/archives/Nicor_535537.zip/inbound/processed/filename

as a file - it doesn't work like that. You need to change things such that you pass an InputStream from the ZipFile (not File) to the Edix ctor when you find the right name as an entry. You will then read it
Incidentally, i don't see where you define HEADER_LENGTH at a quick glance
Avatar of g46905

ASKER

CEHJ,
       I am currently working on making changes so that we can pass an inputstream instead of the filename. I will inform you what I see. Thanks for the advice.

Thanks
You will need to unzip the files first in order to pass a file as a parameter: http://www.devx.com/getHelpOn/10MinuteSolution/20447
Avatar of g46905

ASKER

CEHJ,
       I nticed that you have suggested the code mentioned below.This would owrk if the file is file object.But, I am try to access the file within  the zip file. I believe that the file within the zip file can only be accessed as an input stream.Can you please give me your valueble suggestion?

Thanks

/**
      *  Gets the lines beginning with 'ST'
      *
      * @param  startDir         Directory in which to start
      * @return                  All lines required
      * @exception  IOException  Description of the Exception
      */
     private java.util.List getLines(String startDir) throws IOException {
          java.util.List list = new Vector();
          File dir = new File(startDir);
          File[] files = dir.listFiles();
          for (int i = 0; i < files.length; i++) {
               // loop through and read
               BufferedReader in = new BufferedReader(new FileReader(files[i]));
               String buffer = in.readLine();
               if (buffer != null && buffer.startsWith("ISA")) {
                    // read the rest
                    while ((buffer = in.readLine()) != null) {
                         if (buffer.startsWith("ST")) {
                              list.add(buffer);
                         }
                    }
               }
               in.close();
          }
          return list;
     }
Avatar of g46905

ASKER

I was thinking of an option to get through the current problem that I have.

If I want to access the file within the zip file as a file object instead of an input stream.The only solution that I can think of is to unzip the file into a temp folder and then read it from there as a file. Can someone help me with the code requred to get this done?

Thanks
Avatar of g46905

ASKER

girionis,
           I tried the example you metioned in the link below.

http://www.devx.com/getHelpOn/10MinuteSolution/20447

I am trying to unzip the file and place the content of the file(for example inbound/processed/699201_699195_108567560701023) into a temp directory soa that I can pass 699201_699195_108567560701023 as a file to my class and read the necessary data.I still get this error

Extracting file: inbound/error/699201_699195_108567560701023
Unhandled exception:
java.io.FileNotFoundException: inbound/error/699201_699195_108567560701023 (The system cannot find the path specified)
      at java.io.FileOutputStream.open(Native Method)
      at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
      at java.io.FileOutputStream.<init>(FileOutputStream.java:76)
      at com.report.Unzip.main(Unzip.java:50)


Can some one please tell me what I need to change.

Thanks,
Can you try with some other zip file? I tried the exact same example and it is working fine here.
Avatar of g46905

ASKER

Thanks for all your help!!
8-)