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*00000 8218*0*P*:
GS*FA*123456789SQOP*NOSQO3 4563456*20 040624*212 0*1382*X*0 04030
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,
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*00000
GS*FA*123456789SQOP*NOSQO3
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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*40213 1012*0*P*:
GS*PO*987654321*NNOMS98765 4321*04021 3*1012*402 131012*X*0 03040
ST*850*402131012
BEG*00*G1*X0402131012**040 213
DTM*102*****DT*20040213101 2
N1*SJ**1*123456789
N1*78**1*987654321
PO1*000001*****CR*105341*M N*P
DTM*007*****D8*20040214
DTM*197*****D8*20040229
SLN*000001*10043612001*I*1 0000*BZ
SI*AP*QT*R*TT*01*PG*653957 05900101*R 2*1*UK*T20 69F*R3*1*D K*ABCDE
N1*M2**29*4669
N1*MQ**29*3811
N1*US**1*333333333
N1*DW**1*222222222
ST*850*402131012
PO1*000002*****CR*110014*M N*P
DTM*007*****D8*20040214
DTM*197*****D8*20040229
SLN*000001*10043612002*I*1 0000*BZ
SI*AP*QT*R*TT*01*PG*641487 05900101*R 2*1*UK*X00 018*R3*1*D K*ABCDE
N1*M2**29*4669
N1*MQ**29*3811
N1*US**1*111111111
N1*DW**1*987654321
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*40213
GS*PO*987654321*NNOMS98765
ST*850*402131012
BEG*00*G1*X0402131012**040
DTM*102*****DT*20040213101
N1*SJ**1*123456789
N1*78**1*987654321
PO1*000001*****CR*105341*M
DTM*007*****D8*20040214
DTM*197*****D8*20040229
SLN*000001*10043612001*I*1
SI*AP*QT*R*TT*01*PG*653957
N1*M2**29*4669
N1*MQ**29*3811
N1*US**1*333333333
N1*DW**1*222222222
ST*850*402131012
PO1*000002*****CR*110014*M
DTM*007*****D8*20040214
DTM*197*****D8*20040229
SLN*000001*10043612002*I*1
SI*AP*QT*R*TT*01*PG*641487
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
http://javaalmanac.com/egs/java.util.zip/GetZip.html
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/Nico r_535537.z ip/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(rc o.getDirec tion())!= -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];
}
}
************************** ********** ********** ********** ********** ********** *******
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/Nico
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(rc
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
if ("ISA".equals(s.substring(
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];
}
}
**************************
ASKER
This is the error I am getting when I try to read the file in the EdiX12 class after unzipping it.
java.io.FileNotFoundExcept ion: inbound\processed\inbound/ processed/ X12.bpp
(The system cannot find the path specified)
This is the actual file name "C:/Reporting/archives/Nic or_535537. zip/inboun d/processe d/X12.bpp"
java.io.FileNotFoundExcept
(The system cannot find the path specified)
This is the actual file name "C:/Reporting/archives/Nic
>java.io.FileNotFoundExcep tion: 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.
>(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.
ASKER
It is in the C: drive under Reporting/archives/Nicor_5 35537.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
"/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?
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
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?
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?
>>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?
ASKER
This is what I was trying to do. i am reading the zip file name(C:/Reporting/archives /Nicor_535 537.zip) from the command line. Once you open the Nicor_535537.zip file, we have the following file structure
"/inbound/processed/filena me"
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
"/inbound/processed/filena
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/filena me"
OK. At best, that will try to open the file C:\inbound\processed\filen ame. Is that what you want?
OK. At best, that will try to open the file C:\inbound\processed\filen
ASKER
No. I want it to open "C:/Reporting/archives/Nic or_535537. zip/inboun d/processe d/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
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,
Thanks,
>>Nicor_535537.zip
Is that really a directory?
Is that really a directory?
ASKER
That is one of the zip file in our archives.Within the zip file, we have /inbound/processed/filenam e. I really appreciate all your patience in this issue.
Thanks again,
Thanks again,
Then you can't open
C:/Reporting/archives/Nico r_535537.z ip/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
C:/Reporting/archives/Nico
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
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
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
OK. I see this is open. What happened here?
https://www.experts-exchange.com/questions/21101500/Using-HashMap-and-return-a-key-by-giving-the-value.html
https://www.experts-exchange.com/questions/21101500/Using-HashMap-and-return-a-key-by-giving-the-value.html
You will need to unzip the files first in order to pass a file as a parameter: http://www.devx.com/getHelpOn/10MinuteSolution/20447
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;
}
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;
}
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
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
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_6 99195_1085 6756070102 3) into a temp directory soa that I can pass 699201_699195_108567560701 023 as a file to my class and read the necessary data.I still get this error
Extracting file: inbound/error/699201_69919 5_10856756 0701023
Unhandled exception:
java.io.FileNotFoundExcept ion: inbound/error/699201_69919 5_10856756 0701023 (The system cannot find the path specified)
at java.io.FileOutputStream.o pen(Native Method)
at java.io.FileOutputStream.< init>(File OutputStre am.java:11 6)
at java.io.FileOutputStream.< init>(File OutputStre am.java:76 )
at com.report.Unzip.main(Unzi p.java:50)
Can some one please tell me what I need to change.
Thanks,
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_6
Extracting file: inbound/error/699201_69919
Unhandled exception:
java.io.FileNotFoundExcept
at java.io.FileOutputStream.o
at java.io.FileOutputStream.<
at java.io.FileOutputStream.<
at com.report.Unzip.main(Unzi
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.
ASKER
Thanks for all your help!!
8-)
:)
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();