Home » Core Java » io » FileOutputStream » Write byte array to file with FileOutputStream

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Write byte array to file with FileOutputStream

With this example we are going to demonstrate how to write a byte array to a file using a FileOutputStream. The FileOutputStream is an output stream for writing data to a File or to a FileDescriptor. In short, to write a byte array to a file using a FileOutputStream you should:

  • Create a new File instance by converting the given pathname string into an abstract pathname.
  • Create a new FileOutputStream to write to the file represented by the specified File object.
  • Write bytes from a specified byte array to this file output stream, using write(byte[] b) API method.
  • Don’t forget to close the stream, using its close() API method.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteByteArrayToFileWithileOutputStream {
	
	public static void main(String[] args) {
		
		String s = "Java Code Geeks - Java Examples";
		
		File file = new File("outputfile.txt");
		
		FileOutputStream fos = null;

		try {
			
			fos = new FileOutputStream(file);
			
			// Writes bytes from the specified byte array to this file output stream 
			fos.write(s.getBytes());

		}
		catch (FileNotFoundException e) {
			System.out.println("File not found" + e);
		}
		catch (IOException ioe) {
			System.out.println("Exception while writing file " + ioe);
		}
		finally {
			// close the streams using close method
			try {
				if (fos != null) {
					fos.close();
				}
			}
			catch (IOException ioe) {
				System.out.println("Error while closing stream: " + ioe);
			}

		}
		
	}

}

 
This was an example of how to write a byte array to a file using a FileOutputStream in Java.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of