Solved

reading the content from the input stream

Posted on 2004-09-17
46
241 Views
Last Modified: 2012-05-05
I am trying to read a file in this format.

ISA*00*          *00*          *14*1234567890000  *01*123456789      *040626*0550*U*00304*190641071*0*P*>
GS*FA*123456789*000000000UPRD*040626*0550*19647914*X*003040
ST*000*KR647914
 
The length of the first line is fixed ie 106 bytes
I want to read the whole secont line into a buffer from the inout stream.This length is not fixed.
I was able to do by

reading it from 106 + 1 position until we encounter the end of the line character.Then I found an issue with some of the files.some file have two end of the line character at end of each line.Can some one please give me your valueable solution to get through this situation and read the content of the second line into the string buffer successfully?

Thanks,


0
Comment
Question by:g46905
  • 24
  • 22
46 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12085725
If the first line is a 'line' and the second is a 'line' what's the problem about reading with BufferedReader.readLine?
0
 

Author Comment

by:g46905
ID: 12085803
I would prefer reading the file as an input stream because I am trying to check the first four characters of the file to make sure that this is the file I want to read(for eg: it should start with ISA).
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12085811
... or maybe because of the extra line feed? This should fix it:


BufferedReader in = new BufferedReader(new FileReader("x.txt")); //
String line = null;
boolean finishedReading = false;
String lineTwo = null;
int linesRead = 0;
while ((line = in.readLine()) != null & !finishedReading) {
      if ((line = line.trim()).length() > 0) {
            linesRead++
      }
      else {
            continue;
      }
      if (linesRead == 2) {
            lineTwo = line;
            finishedReading = true;
      }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12085847
Our posts overlapped:

      if ((line = line.trim()).length() > 0) {
            linesRead++;
            finishedReading = (line.startsWith("ISA") == false);
      }
      else {
            continue;
      }
0
 

Author Comment

by:g46905
ID: 12085875
can you please tell me if I can come across this situation by using input stream. I have already built all me logic as I was not expecting to have to linefeed characters at the end of the line. I appreciate your help.

Thanks,
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 12085900
Sorry

>>finishedReading = (line.startsWith("ISA") == false);

should have been

finishedReading = (linesRead == 1 && line.startsWith("ISA") == false);

It seems to me that your application logic should rely on reading lines of text

0
 

Author Comment

by:g46905
ID: 12086875
I dont think I will be able to read the file using BufferedReader as the file that I am reading is in a zip folder. Please correct me if I am wrong.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12086904
You can read it fine if it's in a zip
0
 

Author Comment

by:g46905
ID: 12086992
CEHG,
       You mean to say I cannot read the second line by using input stream in my situation? I have already been using my application for quite some time in this way. If I have to use BufferedReader, I need to redesing all my application and need to get this done by the end of the day.Please suggest.

Thanks,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12087054
>>You mean to say I cannot read the second line by using input stream in my situation?

Well you can yes, but it will mean reinventing the wheel. Reading lines of text (which your logic requires), especially cross-platform, is not as trivial a task as it at first appears. I suggest you look at the source for BufferedReader

>>I need to redesing all my application

Really? A BufferedReader's functionality amounts to a superset of the functionality of an input stream
0
 

Author Comment

by:g46905
ID: 12087118
CEHJ,
       As per you valuable suggestion, I will make the changes and let you know what I find. I will get back to you as soon as possible if I fall into any pit on the way.I need to get this done by the end of the day:-)

Thanks again,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12087139
If you want a quick (and inefficient) kludge, scan the file first and eliminate empty lines
0
 

Author Comment

by:g46905
ID: 12087373
CEHJ,
       I tried to replace

f = myzipfile.getInputStream(ze);  // f = input stream

with

BufferedReader in = new BufferedReader(new FileReader(ze));

and this is the error I see

The constructor FileReader(ZipEntry) is undefined.

Can you suggest where to go from here?

Thanks,

0
 

Author Comment

by:g46905
ID: 12087976
CEHJ,
       In java, can I read something from the inputstream and put it back into the stream.

for example

the stream contains

ABCDEFGHIJKLMNOPQRSTUVWXYZ

i want to accept this as a stream, but I want to store this whole line(including 'A') in a String Buffer only if it starts with A.

Can you please suggest?

Thanks,









0
 

Author Comment

by:g46905
ID: 12088559
I am still waiting for the response. Could some please help me with the above mentioned question?

Thanks,
Shekar
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12089173
BufferedReader in = new BufferedReader(new InputStreamReader(myzipfile.getInputStream()));
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12089180
Sorry

BufferedReader in = new BufferedReader(new InputStreamReader(myzipfile.getInputStream(ze)));
0
 

Author Comment

by:g46905
ID: 12115539
CEHJ,
     Normally,the file that I am trying to read can also expect to have '7E' as an end of the line character.In this case,it is throwing an error. Could you please suggest how to handle this situation?

Thanks,


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12116214
>>Normally,the file that I am trying to read can also expect to have '7E' as an end of the line character

Well then it's not a text file. I thought it was ...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12116238
>>The length of the first line is fixed ie 106 bytes

That's not a 'line' if it ends in 0x7E. Sounds like you need to rationalize your data format
0
 

Author Comment

by:g46905
ID: 12116273

Can you please tell me how I can do that?

That
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12116290
You'll have to redesign the format. Can you post the current 'design'?
0
 

Author Comment

by:g46905
ID: 12116639
Here is the method where I am doing my manipulations. Please let me know if you do not follow anything.Thaks for all your help.

public static void unzipAndGetEntries(String zipfilename , Date fromDD , Date toDate, String profileName,String transactionName){
      
          try{
                 Date fromDDD = fromDD;
                 Date toDDD = toDate;
                 byte content[] = new byte[HEADER_LENGTH];
               int size;
               int recv;
               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)))
     
       {
                         
          f = myzipfile.getInputStream(ze);
          recv = f.read(content);
          String isa = new String(content, 0, HEADER_LENGTH);
            if ("ISA".equals(isa.substring(0,3))) {
                             
                char ch;
                segmentDelimiter = content[HEADER_LENGTH - 1];
                elementDelimiter = content[3];
                char segDelim = (char) segmentDelimiter;
                char elemDel = (char) elementDelimiter;
                  size = (int)ze.getSize();
                  StringBuffer gs = new StringBuffer();
                  Date rD = null;
                   
               do {
                 
                   ch =(char)f.read();
                   gs.append(ch);
                 
               
         } while (ch!= segmentDelimiter);
         
                             
     // date parsing              
         int indx = 0;
         int begDate = 0;
         int endDate = 0;
         String  gsValue = null;
         String  date = null;
         for (int i = 0; i < gs.length(); i++) {
                if (gs.charAt(i) == elemDel) {
                indx = indx + 1;
                if (indx == 3){gsValue = gs.substring((i+1),(i + 14));}
                if (indx == 4){begDate = i;}
                if (indx == 5){endDate = i;}
                                
                }// if
         }// for
         
                  
               date = gs.substring((begDate + 1),endDate);
               int dateLength = endDate - begDate ;  
               
         try {
                  
                   java.text.SimpleDateFormat formatter = null;
                   if (dateLength == 7){formatter = new java.text.SimpleDateFormat("yyMMdd");}
                   if (dateLength == 9){formatter = new java.text.SimpleDateFormat("yyyyMMdd");}
                  
                   rD = formatter.parse(date);
                            
                   if (((gs.substring(0,gs.length()-1).indexOf(transactionName))!=-1)
                   
                         && (rD.after(fromDDD) && rD.before(toDate))){
                     
                              testList.add(new String(gs.substring(0,gs.length()-1)));
                          processZipEntry(f, size,segDelim,elemDel,fromDDD,toDDD,profileName); } // end of if
                    
                    rD = null;
                gs = null;
                      
                   }   catch (java.text.ParseException pe ) {
                        System.out.println(" Error while parsing the file :");
                        pe.printStackTrace();} // end of try
                        
               } // end of if (check file extension)
          
            }   // if condition  
      
      
                }  // end of enumeration
             
        myzipfile.close();
        f = null;
    }catch (IOException e){ System.out.println("Error while unzipping the files : " );
            e.printStackTrace();}


}// unzipAndGetEntries()      
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 86

Expert Comment

by:CEHJ
ID: 12116668
It's documentation of the file format that i need, not the code
0
 

Author Comment

by:g46905
ID: 12116809
Oooh, I will send it to you ASAP

Thanks,
0
 

Author Comment

by:g46905
ID: 12117037
This is the only information I have currently, I am trying to build the application basing upon these specs.

ISA*00*          *00*          *14*1234567890000  *01*123456789      *040626*0550*U*00304*190641071*0*P*>
GS*FA*123456789*000000000UPRD*040626*0550*19647914*X*003040
ST*000*KR647914


I am trying to read the file which is in the X12 format.
This file can be identified only by checking the first
three characters of the file which is "ISA".
The 106th character in the file can always be treated as the segment delimiter(end of line character).
If the 107th is 'G',I would like to store it into a string buffer.
After parsing the date, and the required information from the GS line,
I would like to store the rest of the file into a vector to run my report.


Please let me know if you have additional questions.

Thanks a lot!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12117307
>>I would like to store it into a string buffer.

What does 'it' refer to?
0
 

Author Comment

by:g46905
ID: 12118330
it referes to "GS*FA*123456789*000000000UPRD*040626*0550*19647914*X*003040" . Please let me know if you have any questions.

Thanks,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12118385
Is the bit that begins with GS always a line in the normal sense? If not, what is its end delimiter?
0
 

Author Comment

by:g46905
ID: 12119373
It can be anything

for example

0A,0D,7E but cannot be the same as element seperator( * in the above example). I only way we can know the delimiter is by checking the 106th chanracter.I having problem only when some one sends file using dos format. It contains an additional character at the end of the line. Please let me know if you have any questions.

Thanks,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12122290
OK. So are you saying that the end point of the 'line' beginning 'GS' (we'd better use the word 'chunk' in talking about this) is delimited by the *same* character as found at char 106?
0
 

Author Comment

by:g46905
ID: 12122329
you are correct

Thanks,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12122348
>>After parsing the date, and the required information from the GS line,

OK - this may sound obvious, but

a. where's the date to be found
b. what's 'the required information'?
0
 

Author Comment

by:g46905
ID: 12122387
If you have already noticed the code that I posted yesterday, I am parsing the date from the line which is starting with 'GS'.My whole idea of this application to get the number of transactions from our archives depending upon the user's selection of fromDate and toDate.

I am using GS information to determine which profile it belongs to

Number of transaction  = Number of STs

Please let me know if you have any questions

Thanks,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12122457
Yes, i know. I'm just asking you from a non-code point of view, how you see the date is to be found
0
 

Author Comment

by:g46905
ID: 12122513
This part of GS line *040626*0550* helps us find the date. I am not sure if I answered your question. Please let me know if this is not clear.

Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12122775
It's not really ;-) Got to go now but shall resume later
0
 

Author Comment

by:g46905
ID: 12144859
Please have a look at the code below. I want it to iterate until the  'f' value becomes null.I tried to replace "if (f != null) {"

with "while (f != null) {" and it is throwing an error. Can you please give me your valueable suggestion.




public static void processZipEntry(InputStream f, int size,char segDel,char elemDel ,Date fromDDDt,Date toDDDt,String profName ) {
      
      Vector v = new Vector();
      StringBuffer sb = new StringBuffer();
          try {
                        
                  String stSlnValue = null;
                
                int sBegin = 0;
                size = size - HEADER_LENGTH;
                
                
          
             if (f != null) {
                        char ch = (char)f.read();
                        while ((ch = (char) f.read()) != -1 && ch != segDel) {
                if (ch != '\r' && ch != '\n')
                    sb.append((char)ch);
                    stSlnValue = sb.toString();
                  }
                  v.add(new String(stSlnValue));
              
              
                       //System.out.println( " STSLN Value : " + stSlnValue);
                   if (ch == segDel) {
                         sb.append((char)ch);
                         if (isDoubleSeparator)
                     sb.append((char) f.read());
                         //System.out.println(" (char) f.read()" + (char) f.read() );
                         sb = null;
        
        
         //System.out.println( " STSLN Value : " + stSlnValue);
           }
                 //System.out.println(" sb : " + sb.toString());
          
      }  
      
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12147880
It won't ever be null, it's a reference to the stream itself

Try and clarify the date bit - post the bit where the date is, and surround it by $. Like

$blah$
0
 

Author Comment

by:g46905
ID: 12170214
CEHJ,
      Could you please tell me how do I print the values in the java.util.List list in the code?

try {
      java.util.List list = new Vector();
      File in_dir = new File(filePath);
      File filelist[] = in_dir.listFiles();
      for(int x = 0; x < filelist.length; x++)
       {
                         if(filelist[x].getName().equals(fileName)){
             System.out.println( "File name : " + fileName);
             BufferedReader in = new BufferedReader(new FileReader(filelist[x]));
             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();
        }
       }
            
}catch(Exception e){e.printStackTrace();}

Thanks,
            
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12174686
>>how do I print the values in the java.util.List list in the code?

        in.close();
    }
System.out.println(list);
 

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12213184
8-)
0
 

Author Comment

by:g46905
ID: 12213191
You deserve this for your patience in answering my questions.:-)

Thanks again.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12213204
No problem. Have you got it working now?
0
 

Author Comment

by:g46905
ID: 12213211
Yes sir. I have changed the logic so that it can handle files evern in dos format.

Thanks,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12213221
Very good ;-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

762 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

21 Experts available now in Live!

Get 1:1 Help Now