• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Java FileOutputStream Example

April 19, 2014 //  by Krishna Srinivasan//  Leave a Comment

This example discuss about the FileOutputStream. This class extends from the OutputStream and used for writing the stream of bytes into a file. This class has the following constructors for creating the instance.

  • FileOutputStream(File file) – Creates a file output stream to write to the file represented by the specified File object.
  • FileOutputStream(File file, boolean append) – Creates a file output stream to write to the file represented by the specified File object.
  • FileOutputStream(FileDescriptor fdObj) – Creates a file output stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.
  • FileOutputStream(String name) – Creates a file output stream to write to the file with the specified name.
  • FileOutputStream(String name, boolean append) – Creates a file output stream to write to the file with the specified name.

Also this class has the following overloaded methods for writing the streams to a file.

  • void write(byte[] b) – Write all the bytes of byte array b in the destination resource.
  • void write(byte[] b, int off, int len) – Write a sub sequence of the byte array.
  • void write(int b) – Write a single byte.

Lets look at the simple example on how to use FileOutputStream for writing to a file.

package javabeat.net.core;

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

/**
 * Java FileOutputStream Example
 *
 * @author Krishna
 *
 */
public class FileOutputStreamExample {
	public static void main (String args[]) throws IOException{
		//Creating file instance
		File file = new File("TextFile.txt");

		//Creating file output stream instance
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		String str = "This is example for FileOutputStream";

		//Gets byte array
		byte b[] = str.getBytes();

		//Writing byte array to a file
		fileOutputStream.write(b);

		//Closing the stream
		fileOutputStream.close();
	}
}

If you run the above program, the file named “TextFile.txt” will be created in the same director with the given content.

Category: JavaTag: Java File IO

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « PrimeFaces Collector Example
Next Post: Java BufferedOutputStream Example »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact