dcsimg
reading input
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   prasanna_srinivasan
Posted On:   Friday, April 18, 2008 04:24 AM

which stream object must be used if we want to read every word in a file as a separate string and write it to another file please give the implementation if possible


Please use html tags to format code blocks.

Re: reading input

Posted By:   Stephen_McConnell  
Posted On:   Monday, April 21, 2008 10:08 AM

I would use the java.io.BufferedReader class to manage it and if you are using a File, I would use a java.io.FileReader as my reader. One doesn't need to worry about dealing with "streams" if you do that.


Something like (there are better ways to do this... I'm using old Java 2.0 syntax):




File myFile = new File("path to file");
BufferedReader br = null;

// Somewhere here you would define your
// file output, also.

try {

br = new BufferedReader(new FileReader(myFile));

String line = null;
String words = null;
while( (line = br.readLine()) != null){
// create an array of words split by a space.
words = line.split(" ");
for(int i = 0; i < words.length; i++){
// write out the word at words[i]

}


}



}catch(IOException e){

// Handle any io exceptions.
}finally{
// close you input and output files.
}



This is something like what one could do. You have the language take care of the messy character buffering and reading of individual characters etc etc that one would do with a stream.


Hope this helps.


Stephen McConnell

About | Sitemap | Contact