Credits

Friday, January 14, 2011

Writing From a File Stream to an Output Stream

In my previous posts, we have allowed our input source to be a file and the destination to be another file.

The program below allows us to read from our data file and write the characters on the screen as our output stream.


[+/-] show/hide


/*prints from a given file to an output stream*/
import java.io.*;

public class ReadFromToStream {

public static void main (String[] args) throws IOException{
//InputStream istream;
InputStream fstream;
OutputStream ostream;
int c;
File fromFile=new File("\\Documents and Settings\\Rose\\My Documents\\NetBeansProjects\\SampleExercises\\src\\data.txt");
ostream=System.out;
fstream=new FileInputStream(fromFile);
try{

while ((c=fstream.read()) != -1){
ostream.write (c);
}
}catch (IOException e)
{System.out.println("Error :"+e.getMessage());}
finally {
fstream.close();
ostream.close(); }
}
}


AddThis