reading the content from the input stream

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,


g46905Asked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
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
 
CEHJCommented:
If the first line is a 'line' and the second is a 'line' what's the problem about reading with BufferedReader.readLine?
0
 
g46905Author Commented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
CEHJCommented:
... 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
 
CEHJCommented:
Our posts overlapped:

      if ((line = line.trim()).length() > 0) {
            linesRead++;
            finishedReading = (line.startsWith("ISA") == false);
      }
      else {
            continue;
      }
0
 
g46905Author Commented:
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
 
g46905Author Commented:
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
 
CEHJCommented:
You can read it fine if it's in a zip
0
 
g46905Author Commented:
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
 
CEHJCommented:
>>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
 
g46905Author Commented:
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
 
CEHJCommented:
If you want a quick (and inefficient) kludge, scan the file first and eliminate empty lines
0
 
g46905Author Commented:
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
 
g46905Author Commented:
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
 
g46905Author Commented:
I am still waiting for the response. Could some please help me with the above mentioned question?

Thanks,
Shekar
0
 
CEHJCommented:
BufferedReader in = new BufferedReader(new InputStreamReader(myzipfile.getInputStream()));
0
 
CEHJCommented:
Sorry

BufferedReader in = new BufferedReader(new InputStreamReader(myzipfile.getInputStream(ze)));
0
 
g46905Author Commented:
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
 
CEHJCommented:
>>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
 
CEHJCommented:
>>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
 
g46905Author Commented:

Can you please tell me how I can do that?

That
0
 
CEHJCommented:
You'll have to redesign the format. Can you post the current 'design'?
0
 
g46905Author Commented:
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
 
CEHJCommented:
It's documentation of the file format that i need, not the code
0
 
g46905Author Commented:
Oooh, I will send it to you ASAP

Thanks,
0
 
g46905Author Commented:
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
 
CEHJCommented:
>>I would like to store it into a string buffer.

What does 'it' refer to?
0
 
g46905Author Commented:
it referes to "GS*FA*123456789*000000000UPRD*040626*0550*19647914*X*003040" . Please let me know if you have any questions.

Thanks,
0
 
CEHJCommented:
Is the bit that begins with GS always a line in the normal sense? If not, what is its end delimiter?
0
 
g46905Author Commented:
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
 
CEHJCommented:
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
 
g46905Author Commented:
you are correct

Thanks,
0
 
CEHJCommented:
>>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
 
g46905Author Commented:
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
 
CEHJCommented:
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
 
g46905Author Commented:
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
 
CEHJCommented:
It's not really ;-) Got to go now but shall resume later
0
 
g46905Author Commented:
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
 
CEHJCommented:
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
 
g46905Author Commented:
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
 
CEHJCommented:
>>how do I print the values in the java.util.List list in the code?

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

0
 
CEHJCommented:
8-)
0
 
g46905Author Commented:
You deserve this for your patience in answering my questions.:-)

Thanks again.
0
 
CEHJCommented:
No problem. Have you got it working now?
0
 
g46905Author Commented:
Yes sir. I have changed the logic so that it can handle files evern in dos format.

Thanks,
0
 
CEHJCommented:
Very good ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.