• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Re: FileWriter write operation

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to read in contents from a file, use STringTokenizer to tokenize the file using "/" as the delimiter, and print the contents to another file. I am able to read in the file, tokenize it and even print it to the standard output, but am not able to print it to an output file. The output file is created, but empty. Could anyone please tell me what I am missing here?

Code:

import java.io.*;
import java.util.StringTokenizer;
class Tokenize{
public static void main(String[] args){
try{
FileReader reader = new FileReader("Input file name")/read in the file
BufferedReader buf = new BufferedReader(reader);//reading the contents into a buffer
FileWriter writer = new FileWriter("Output file name");
//PrintWriter out = new PrintWriter(writer);
String line = null;
String val = null;
while((line=buf.readLine())!=null){
StringTokenizer parser = new StringTokenizer(line, "/");//parse with ":" as the delimiter
while(parser.hasMoreTokens()){
//System.out.println(parser.nextToken());
val = parser.nextToken();
writer.write(val);
//System.out.println(val);

//writer.write(parser.nextToken());
}

}
}
catch(Exception e){
e.printStackTrace();
}
}
}
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Could anyone please tell me what I am missing here?

writer.close();
 
venkat venkataraman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Michael, it worked. I didn't realize that I missed close().....
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic