Using FileInputStream, FileOutputStream, and File classes. : Stream « File « SCJP






import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
   
public class MainClass{
 public static void main(String args[]) throws IOException {
  FileOutputStream outStream = new FileOutputStream("test.txt");
  String s = "This is a test.";
  for(int i=0;i<s.length();++i){
   outStream.write(s.charAt(i));
  }
  
  outStream.close();


  FileInputStream inStream = new FileInputStream("test.txt");
  int inBytes = inStream.available();
  System.out.println("inStream has "+inBytes+" available bytes");
  byte inBuf[] = new byte[inBytes];
  int bytesRead = inStream.read(inBuf,0,inBytes);
  System.out.println(bytesRead+" bytes were read");
  System.out.println("They are: "+new String(inBuf));
  inStream.close();
  File f = new File("test.txt");
  f.delete();
 }
}
inStream has 15 available bytes
15 bytes were read
They are: This is a test.








9.3.Stream
9.3.1.InputStream and OutputStream
9.3.2.Low-Level Streams: FileInputStream
9.3.3.A filter connects to an InputStream or OutputStream and performs some transformation on the data
9.3.4.Construct a chain of DataOutputStream, BufferedOutputStream and FileOutputStream
9.3.5.Classes derived from Reader and Writer take locale information when converting between Unicode and other character systems.
9.3.6.A small input chain between FileInputStream and DataInputStream
9.3.7.Input chain between FileOutputStream and DataOutputStream
9.3.8.Using FileInputStream, FileOutputStream, and File classes.