A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/io/fileoutputstream_write.htm below:

Java - FileOutputStream write(int b) method

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 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

java_io_fileoutputstream.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4