Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse whole file with StringTokenizer 1

Status
Not open for further replies.

Elle

Programmer
Sep 19, 2000
20
0
0
GB
Hi Guys,

How do i get my program to read in more than the first line of the file. So far it reads in all of the lines from the input file one by one and then it is meant to parse it and output all the tokens to a txt file. But at the moment it only outputs the first line. I tried putting another for loop around the while(st.hasMoreTokens()){ part but it didn't work.

here's my code...

public class Test {
public static void main(String[] args) throws IOException {

File output = new File("N:/output.txt");
FileWriter out = new FileWriter(output);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("N:/Printer.txt"))));

String line = reader.readLine();
StringTokenizer st = new StringTokenizer(line, ",;:");



for(int i =0; line != null; i++) { //Keep on reading in lines until EOF
System.out.println(line);
line = reader.readLine(); //reading in all lines ok

while(st.hasMoreTokens()){ //while there are still tokens to read

out.write(st.nextToken()); //output token to file

} i++;
}
out.close();
}
}

 
You have to call readLine() for each line in the file. Add:

line = reader.readLine();

to the bottom of your outer loop.
 
Instead of :
for(int i =0; line != null; i++) { //Keep on reading in lines until EOF
System.out.println(line);
line = reader.readLine(); //reading in all lines ok

while(st.hasMoreTokens()){ //while there are still tokens to read

out.write(st.nextToken()); //output token to file

} i++;
}

use :

Code:
String line = "";
while ((line = reader.readLine()) != null) {
  //do stuff with StringTokenizer
}
 
Ok Cheers that worked. But now it's not outputting any of the tokens to the output file. It doesn't seem to be getting into the now second while loop (//while there are still tokens to read). I tested it by making it output something to the file once it enters the loop but the file remained empty.

Any ideas?
 
Are you flushing and closing the streams ?
 
I don't flush the stream but i close it. My code is posted in the very first message of this post, it's still the same apart from slight alterations. I close the output filewriter after i've finished doing all the stuff to it. Should i be flushing it?
 
Its good practive to flush before close.

Your StringTokenizer should be within the while() loop reading the lines from the file ...

Code:
String line = "";
while ((line = reader.readLine()) != null) {
  //do stuff with StringTokenizer
   StringTokenizer st = new StringTokenizer(line, ",;:");
   while(st.hasMoreTokens()){ //while there are still tokens to read                                                 
             
     out.write(st.nextToken()); //output token to file
}
 
Brilliant thankyou very much. I put the alteration in first and still nothing. So i then added in the flush and low and behold all the tokens came flowing out!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top