Java FileOutputStream ClassLast Updated : 22 Jan 2026 The FileOutputStream class in Java is used to write byte data to a file. In this chapter, we will learn what the FileOutputStream class is, when to use it, its methods, and how to write data to a file using simple examples. What is FileOutputStream in Java?The FileOutputStream class is an output stream used to write data into a file. It writes data in the form of bytes and is mainly used for byte-oriented file operations. If you want to write primitive or binary data into a file, the FileOutputStream class is suitable. Although it can also write character data, it is recommended to use the FileWriter class for character-oriented operations. FileOutputStream class declarationThe following declaration shows how the FileOutputStream class is defined: Constructors of FileOutputStream ClassThe FileOutputStream class provides constructors to create an output stream and connect it to a file. 1. FileOutputStream(String name)This constructor creates a file output stream to write data to a file with the specified name. If the file already exists, its content is overwritten. If the file does not exist, a new file is created. Syntax: Here is the syntax of this constructor: 2. FileOutputStream(String name, boolean append)This constructor creates a file output stream to write data to a file. If append is set to true, the data is added at the end of the file. If it is false, the existing file content is overwritten. Syntax: Here is the syntax of this constructor: 3. FileOutputStream(File file)This constructor creates a file output stream using a File object instead of a file name. Syntax: Here is the syntax of this constructor: 4. FileOutputStream(File file, boolean append)This constructor creates a file output stream using a File object and allows appending data to the file if required. Syntax: Here is the syntax of this constructor: Example: Using FileOutputStream Constructor with Append ModeThe following example demonstrates how to append data to an existing file using the FileOutputStream constructor. Output: data appended successfully Methods of FileOutputStream ClassThe FileOutputStream class provides several methods to write data to a file.
Example 1: Writing a Single Byte Using FileOutputStreamIn this example, a single byte value is written to a file. The byte value 65 represents the character A. Output: Success... The content of a text file testout.txt is set with the data A. testout.txt A Example 2: Writing a String Using FileOutputStreamIn this example, a string is converted into a byte array and then written to a file using the FileOutputStream class. Output: Success... The content of a text file testout.txt is set with the data Welcome to javaTpoint. testout.txt Welcome to javaTpoint. Next TopicJava FileInputStream Class |
We request you to subscribe our newsletter for upcoming updates.