Java.io - SPLessons

Java.io FileOutputStream

Home > Lesson > Chapter 16
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io FileOutputStream

Java.io FileOutputStream

shape Introduction

Java.io FileOutputStream is the class used to write a data to  the file. Java.io FileOutputStream writes streams of bytes to the file such as data related to image. For writing streams of characters to file, then file writer is used instead of putStream.

Class Declaration

shape Declaration

Java.io FileOutputStream class is declared as follows: Public class FileOutputStream extends OutputStreams

Class Constructors

shape Table

Constructor Description
FileOutputStream (File file) A file output stream is created by this constructor and writes to the represented file object.
FileOutputStream (String name) A file output stream is created by this constructor and writes to the represented name.
FileOutputStream  (FileDescriptor fdObj) A file output stream object is created by this constructor and used the file descriptor to represent the connection of actual file to write.
FileOutputStream (String name, boolean append) A file output stream is created by this constructor to write into file represented by name.

Class Methods

shape Table

Method Description
void close() By using this method the file output stream is closed and releases the another resources related to this stream.
void write(byte[] b) By using this method it writes the data up to ‘n’ bytes from the output stream into an array of bytes.
void write(int b) By using this method it writes the data to the file output stream.
void write(byte[] b, int off, int n) By using this method it reads the data up to ‘n’ bytes from the output stream into an array of bytes starting at offset of this file output stream.
protected void finalize() By using this method it can close the output stream at that time close method is called, only when there are no more actions to perform.
FileChannel getChannel() Associating with file output stream the current method returns unique File Channel object.
FileDescriptor getFD() By using this method it returns file descriptor associating with file output stream.

Inherited Methods

shape Description

From the following classes, methods are inherited to Java.io.FileOutputStream
  • Java.io.OutputStream
  • Java.io.Object

shape Examples

Usage of the void write(int b) method. [c]import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileOutputStream { public static void main(String[] args) throws IOException { FileOutputStream fo = null; FileInputStream fi = null; byte b = 65; int j=0; char c; try{ // creating a new file output stream fos=new FileOutputStream("C://test.txt"); // It writes byte to the output stream fos.write(b); // It flushes content to stream fo.flush(); // creating new file input stream fi = new FileInputStream("C://test.txt"); // Reads entire content while((i=fi.read())!=-1) { // converting integer to character c=(char)i; // prints System.out.print(c); } }catch(Exception ex) { // if an error occurs ex.printStackTrace(); }finally { // from the stream releases and closes system resources if(fo!=null) fo.close(); if(fi!=null) fi.close(); } } }[/c] The output will be as follows. [c]Output : A[/c] Usage of fileDescriptor getFD() method. [c]import java.io.FileDescriptor; import java.io.FileOutputStream; import java.io.IOException; public class FileOutputStream { public static void main(String[] args) throws IOException { FileOutputStream fo = null; FileDescriptor fd = null; boolean bl = false; try{ // creating new file output stream fs=new FileOutputStream("C://test.txt"); // get file descriptor object fd = fs.getFD(); // testing the file is valid or not bl = fd.valid(); // print the file is valid or not System.out.print(" file is valid? "+bl); }catch(Exception ex) { // if an error occurs ex.printStackTrace(); }finally if(fo!=null) fo.close(); } } }[/c] The output will be as follows. [c]file is valid? true[/c]

Summary

shape Key Points

  • Java.io FileOutputStream is the class used to write a data to  the file.
  • File output stream writes streams of bytes to the file such as data related to image.