FileOutputStream class

                                     
    java.lang.Object                                
     |                               
     |--java.io.OutputStream                            
         |                           
         |--java.io.FileOutputStream                        
                                     

A file output stream is an output stream for writing data to a File or to a FileDescriptor.

ConstructorSummary
FileOutputStream(File file)Creates a file output stream to write to the file represented by the specified File object.
FileOutputStream(File file, boolean append)Creates a file output stream to write to the file represented by the specified File object.
FileOutputStream(FileDescriptor fdObj)Creates an output file stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.
FileOutputStream(String name)Creates an output file stream to write to the file with the specified name.
FileOutputStream(String name, boolean append)Creates an output file stream to write to the file with the specified name.

ReturnMethodSummary
voidclose()Closes this file output stream and releases any system resources associated with this stream.
FileChannelgetChannel()Returns the unique FileChannel object associated with this file output stream.
FileDescriptorgetFD()Returns the file descriptor associated with this stream.
voidwrite(byte[] b)Writes b.length bytes from the specified byte array to this file output stream.
voidwrite(byte[] b, int off, int len)Writes len bytes from the specified byte array starting at offset off to this file output stream.
voidwrite(int b)Writes the specified byte to this file output stream.
Revised from Open JDK source code

Create FileOutputStream object from File object


import java.io.File;
import java.io.FileOutputStream;

public class Main {
  public static void main(String[] args) throws Exception {
    File file = new File("C:/demo.txt");
    FileOutputStream fos = new FileOutputStream(file);
  }
}

Write file using FileOutputStream


import java.io.FileOutputStream;

public class Main {

  public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream("C:/demo.txt");

    byte b = 01;
    fos.write(b);
    fos.close();
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.