File IO : FileOutputStream « File Input Output « Java






File IO

    
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileIOApp {
  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();
  }
}

   
    
    
  








Related examples in the same category

1.Copy Bytes between FileInputStream and FileOutputStream
2.Copy byte between FileInputStream and FileOutputStream
3.Write byte array to a file using FileOutputStream
4.Create DataOutputStream from FileOutputStream
5.Write double to a file using DataOutputStream
6.Append output to file using FileOutputStream
7.Create FileOutputStream object from File object
8.Create FileOutputStream object from String file path
9.Write file using FileOutputStream
10.Use FileOutputStream to write the bytes to a file.
11.Write UTF String, integer and double with DataOutputStream
12.Write data with FileOutputStream
13.Split file
14.Rollover FileOutputStream