3

I'll try to be as clear as possible but pardon me if my question is not perfect. I have a txt file with several lines of data. example:

123 ralph bose 20000 200 1 2

256 ed shane 30000 100 2 4

...

I need to read each line sequentially and pass it back to a method in a separate class for processing. I know how to break down each line into elements by using StringTokenizer.

However, i'm not sure how to read one line at a time, pass back the elements to the other class and then, once the processing is done, to read the NEXT line. Method cooperation between my classes works fine (tested) but how do i read one line at a time?

I was thinking of creating an array where each line would be an array element but as the number of lines will be unknown i cannot create an array as i don't know its final length.

Thanks

Baba

EDIT

rough setup :

Class A

end_of_file = f1.readRecord(emp);

        if(!end_of_file)
        { 
           slip.printPay(slipWrite);
        }

Class B

public boolean readRecord(Employee pers) throws IOException {

        boolean eof = false ;

        String line = in.readLine() ; 

                ???

                }

filename is never passed around

so up until here i can read the first line but i think i need a way to loop through the lines to read them one by one with back and forth between classes.

tricky...

2 Answers2

4

There are lots of ways to read an entire line at a time; Scanner is probably easiest:

final Scanner s = new Scanner(yourFile);
while(s.hasNextLine()) {
    final String line = s.nextLine();
    YourClass.processLine(line);
}
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
  • Hi Micheal, i have added a bit of code to try to illustrate the cooperation roughly. i can't use filename in my setup i think? –  Nov 30 '10 at 15:29
  • 1
    @Baba `yourFile` is actually a [`File`](http://download.oracle.com/javase/6/docs/api/java/io/File.html) instance here, not a filename, but it doesn't really matter. I completely don't understand the code you posted though. This code will read each line from the file and pass that whole line to `YourClass.processLine`; is that what you're looking for? What else does it need to do besides that? – Michael Mrozek Nov 30 '10 at 15:33
  • my problem seems to be the end_of_file boolean. it won't enter the processing (printPay()) until that boolean isn't set to true. However i can't do that until i've read each line so i'm confused as to how i would enter the processing IF statement. i think i need to rework my readRecord method. –  Nov 30 '10 at 15:45
3
void readLine(String fileName)
{
   java.io.BufferedReader br = null;
   try
   {
      br = new java.io.BufferedReader(new java.io.FileReader(fileName));
      String line = null;
      while(true)
      {
          line = br.readLine();
          if(line == null)
             break;
          // process your line here
      }
   }catch(Exception e){
   }finally{
     if(br != null)
      {
         try{br.close();}catch(Exception e){}
       }
   }
}

Also if you want to split strings... use

String classes split method. for splitting depending on space... you can do ... line.split("\\s*")

Hope it works

Favonius
  • 13,959
  • 3
  • 55
  • 95
  • 1
    It's up to you, but a common idiom for situations like this is `while((line = br.readLine()) != null)`, instead of needing to `while(true)` and `break` out – Michael Mrozek Nov 30 '10 at 15:28
  • @Michael: well while coding i follow the standard that you mentioned... but for clarity i have broken down it into extra steps :) – Favonius Nov 30 '10 at 15:32