Credits

Saturday, August 7, 2010

Java Streams and Files

You can use files as source and destination of your text and with streams to act for input and output can carry through your file's contents.

Below is the is another program on streams.

[+/-] show/hide


/*reading from a file*/
import java.io.*;
public class ReadFromFile{
public static void main (String[] args) throws IOException{
InputStream istream;
OutputStream ostream;
int c;
File inFile=new File("\\Documents and Settings\\Rose\\My Documents\\NetBeansProjects\\SampleExercises\\src\\data.txt");
File outFile=new File("\\Documents and Settings\\Rose\\My Documents\\NetBeansProjects\\SampleExercises\\src\\output.txt");
istream= new FileInputStream(inFile);
ostream=new FileOutputStream(outFile);
try{

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

AddThis