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_byte.htm below:

Java - FileOutputStream write(byte[] b) method

Java - FileOutputStream write(byte[] b) method Description

The Java FileOutputStream write(byte[] b) writes an array of bytes to the output file. write(byte[] b) is used for writing binary or text data efficiently using byte arrays instead of writing character by character.

Declaration

Following is the declaration for java.io.FileOutputStream.write(byte[] b) method −

public void write(byte[] b)
Parameters

b − The source buffer.

Return Value

This method does not return any value.

Exception

IOException− If an I/O error occurs.

Example - Usage of FileOutputStream write(byte[] b) method

The following example shows the usage of Java FileOutputStream write(byte[] 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 = {65,66,67,68,69};
      int i = 0;
      char c;
      
      try {
         // create new file output stream
         fos = new FileOutputStream("test.txt");
         
         // writes bytes 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−

ABCDE
Example - Writing a String as Bytes to a File

The following example shows the usage of Java FileOutputStream write(byte[] b) method.

FileOutputStreamDemo.java
package com.tutorialspoint;

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

public class FileOutputStreamDemo {
   public static void main(String[] args) {
      String data = "Hello, FileOutputStream!";

      try (FileOutputStream fos = new FileOutputStream("output.txt")) {
         // Convert string to byte array
         byte[] byteData = data.getBytes();

         // Write byte array to the file
         fos.write(byteData);

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

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

Data written successfully to output.txt.
Explanation Example - Writing an Array of Binary Data (Image or File Content)

The following example shows the usage of Java FileOutputStream write(byte[] 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 = {65, 66, 67, 68, 69}; // ASCII values of 'A', 'B', 'C', 'D', 'E'

      try (FileOutputStream fos = new FileOutputStream("binary_output.txt")) {
         // Writing byte array to the file
         fos.write(binaryData);

         System.out.println("Binary data written to binary_output.txt.");
      } 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.txt.
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