Solved

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

Posted on 2004-08-22
32
183 Views
Last Modified: 2010-03-31
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,

0
Comment
Question by:g46905
  • 14
  • 11
  • 7
32 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 125 total points
ID: 11864836
That's how you can read text:

http://javaalmanac.com/egs/java.io/ReadLinesFromFile.html

then just use the startsWith("ISA") method of the string for the first line you read to see if it start with the "ISA" string.

And this is how you can list all files inside directories (and all their siblings): http://javaalmanac.com/egs/java.io/GetFiles.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11864861
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();
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 125 total points
ID: 11864889
     /**
       *  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;
      }


0
 

Author Comment

by:g46905
ID: 11937482
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



             
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11940000
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
0
 

Author Comment

by:g46905
ID: 11942407
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];
      
      }
      
}

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

0
 

Author Comment

by:g46905
ID: 11942641
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"
0
 
LVL 35

Expert Comment

by:girionis
ID: 11942808
>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.
0
 

Author Comment

by:g46905
ID: 11942974
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
0
 
LVL 35

Expert Comment

by:girionis
ID: 11943422
Can you tell us the line this erro roccurs?
0
 

Author Comment

by:g46905
ID: 11943513
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
0
 
LVL 35

Expert Comment

by:girionis
ID: 11944132
For some reason it can't find the file. What happens if you do:

ifile.exists();

Does it return true or false?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11944922
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?

0
 

Author Comment

by:g46905
ID: 11945064
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11945157
>>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?
0
 

Author Comment

by:g46905
ID: 11945254
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 86

Expert Comment

by:CEHJ
ID: 11945388
Then you'll have to prepend the drive and path to that entry before passing to the ctor
0
 

Author Comment

by:g46905
ID: 11945540
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,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11945698
>>Nicor_535537.zip

Is that really a directory?
0
 

Author Comment

by:g46905
ID: 11945795
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,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11945828
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11945863
Incidentally, i don't see where you define HEADER_LENGTH at a quick glance
0
 

Author Comment

by:g46905
ID: 11945879
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11945921
0
 
LVL 35

Expert Comment

by:girionis
ID: 11949557
You will need to unzip the files first in order to pass a file as a parameter: http://www.devx.com/getHelpOn/10MinuteSolution/20447
0
 

Author Comment

by:g46905
ID: 11953361
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;
     }
0
 

Author Comment

by:g46905
ID: 11956047
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
0
 

Author Comment

by:g46905
ID: 11956453
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,
0
 
LVL 35

Expert Comment

by:girionis
ID: 11970592
Can you try with some other zip file? I tried the exact same example and it is working fine here.
0
 

Author Comment

by:g46905
ID: 12049362
Thanks for all your help!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12049371
8-)
0
 
LVL 35

Expert Comment

by:girionis
ID: 12051734
:)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now