Skip to main content

OutputStream & FileInputStream File Input and Output in JAVA

To use OutputStream class for display a data and FileInputStream for read a data in file.
CODING
import java.io.*;
public class ReadFromFileWriteClass {
public static void main(String[] args) {
int c;
File f1=new File("MyData.txt");
InputStream istream = null;
OutputStream ostream = System.out;
System.out.println("Anything Type");
try{
istream =new FileInputStream(f1);
try{
while((c=istream.read()) !=-1 )
ostream.write(c);
}
catch(IOException e){
System.out.println("Error::"+e.getMessage());
      }
}catch(FileNotFoundException e){
System.exit(1);
  }finally {
   try{
   istream.close();
   ostream.close();
    }catch(IOException e){
System.out.println("Error::File Did Not Close");
     }
  }
}
}

Comments