CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Files

Text and Binary Files
Binary files
Connect a binary output stream to a file

A subset of the OutputStream hierarchy

OutputStream <>
    FileOutputStream
    FilterOutputStream
        BufferedOutputStream
        DataOutputStream <>

Notes

  • All classes in the java.io and java.util.zip packages that end with OutputStream are members of the OutputStream hierarchy.
  • The FilterOutputStream class is a superclass of all classes that filter binary output streams.

Classes used to connect a binary output stream to a file

DataOutputStream	writes data to the stream
	->BufferedOutputStream	creates a buffer for the stream
		->FileOutputStream	connects the stream to a file

Constructors of these classes

Constructor Throws
DataOutputStream(OutputStream) None
BufferedOutputStream(OutputStream) None
FileOutputStream(File[, booleanAppend]) FileNotFoundException
FileOutputStream(StringFileName[, booleanAppend]) FileNotFoundException

A File object that refers to a binary file

File productsFile = new File("products.dat");

How to connect a binary output stream to a file with a buffer

DataOutputStream out = new DataOutputStream(
                       new BufferedOutputStream(
                       new FileOutputStream(productsFile)));

How to connect for an append operation

DataOutputStream out = new DataOutputStream(
                       new BufferedOutputStream(
                       new FileOutputStream(
                           productsFile, true)));
Previous | Binary I/O | Connect a binary output stream to a file | Write to a binary file | Connect a binary input stream to a file | Read from a binary file | Two ways with binary strings | Next