Java - FileOutputStream write(int b) method



Description

The Java FileOutputStream write(int b) writes a single byte to the file output stream. Used when writing one byte at a time (e.g., writing characters or binary data byte by byte).

Declaration

Following is the declaration for java.io.FileOutputStream.write(int b) method −

public void write(int b)

Parameters

b − The byte to be written.

Return Value

This method does not return any value.

Exception

IOException− If an I/O error occurs.

Example - Usage of FileOutputStream write(int b) method

The following example shows the usage of Java FileOutputStream write(int b) method.

FileOutputStreamDemo.java

package com.tutorialspoint;

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

public class FileOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      FileOutputStream fos = null;
      FileInputStream fis = null;
      byte b = 66;
      int i = 0;
      char c;
      
      try {
         // create new file output stream
         fos = new FileOutputStream("test.txt");
         
         // writes byte to the output stream
         fos.write(b);
         
         // flushes the content to the underlying stream
         fos.flush();
         
         // create new file input stream
         fis = new FileInputStream("test.txt");
         
         // read till the end of the file
         while((i = fis.read())!=-1) {
         
            // convert integer to character
            c = (char)i;
            
            // prints
            System.out.print(c);
         }
      } catch(Exception ex) {
         // if an error occurs
         ex.printStackTrace();
      } finally {
         // closes and releases system resources from stream
         if(fos!=null)
            fos.close();
         if(fis!=null)
            fis.close();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

B

Example - Writing ASCII Characters to a File

The following example shows the usage of Java FileOutputStream write(int b) method.

FileOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamDemo {
   public static void main(String[] args) {
      try (FileOutputStream fos = new FileOutputStream("output.txt")) {
         fos.write(72); // ASCII for 'H'
         fos.write(101); // ASCII for 'e'
         fos.write(108); // ASCII for 'l'
         fos.write(108); // ASCII for 'l'
         fos.write(111); // ASCII for 'o'

         System.out.println("Characters written to output.txt.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Characters written to output.txt.

Explanation

  • A FileOutputStream is created for output.txt.

  • The write(int b) method writes individual ASCII characters:

    • 72 → 'H'

    • 101 → 'e'

    • 108 → 'l'

    • 108 → 'l'

    • 111 → 'o'

  • The resulting output.txt file contains: Hello

Example - Writing Binary Data Byte by Byte

The following example shows the usage of Java FileOutputStream write(int b) method.

FileOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamDemo {
   public static void main(String[] args) {
      byte[] binaryData = {10, 20, 30, 40, 50}; // Binary values

      try (FileOutputStream fos = new FileOutputStream("binary_output.bin")) {
         for (int b : binaryData) {
            fos.write(b); // Writing one byte at a time
         }

         System.out.println("Binary data written to binary_output.bin.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output()

Let us compile and run the above program, this will produce the following result−

Binary data written to binary_output.bin.

Explanation

  • A byte array {10, 20, 30, 40, 50} is defined (representing binary values).

  • The write(int b) method writes one byte at a time using a loop.

  • The resulting binary_output.bin file will contain raw binary data (not readable as text).

java_io_fileoutputstream.htm
Advertisements