FileOutputStream class is a subclass of OuputStream abstract class. FileOutputStream is used create an output stream, which is used to write byte/bytes to a file.

Java FileOutputStream is an output stream used for writing data to a file

  • FileOutputStream class Constructor

    Constructor Description
    FileOutputStream(File file) This constructor creates a FileInputStream object to read a file specified by the File object, which is passed to this constructor as a parameter.
    FileOutputStream(String path) This constructor creates a FileOutputStream to write to a file which is accessed by the path mentioned in the parameters of this constructor.
  • FileOutputStream class methods

    Some methods that we generally use while working with FileOutputStream are shown in the table below

    Methods Description
    flush() This method flushes any buffered data to be written out of this output stream.
    write(int b) This method writes one byte at a time to this output stream.
    write(byte[]) This method writes a whole byte array at a time to this input stream.
    close() This method closes this outut stream and also frees any resources connected with this output stream.

    Example : Writing a single byte to file

    public class FileOutput {
        public static void main(String[] args)throws IOException {
            FileOutputStream fos=new FileOutputStream("");
            char ch='A';
            fos.write(ch);//writing the single byte data to file
            System.out.println("Successfully Done....");

    Successfully Done....

    A new file with name has been created .

    Example : writing a string content to file using FileOutputStream.

    public class FileOutput1 {
        public static void main(String[] args) throws IOException {
            FileOutputStream fos = new FileOutputStream("");
            String content = "Java is Object Oriented Programming Language which is basically used to make software applications.";
            byte[] b = content.getBytes();
            System.out.println("Successfully done.............");

    Successfully done.............

What next?

The next topic is ByteArrayInputStream in java

Share this page