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
A String is converted into a byte array using getBytes().
A FileOutputStream object is created to write to output.txt.
The write(byte[]) method writes the entire byte array to the file.
The try-with-resources ensures automatic closure of the file stream.
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
A byte array {65, 66, 67, 68, 69} (representing "ABCDE") is defined.
A FileOutputStream writes this binary data to binary_output.txt.
The file will contain the text ABCDE when opened in a text editor.
The try-with-resources ensures the stream is closed automatically.
java_io_fileoutputstream.htm