Package Package provides for system input and output through data streams, serialization and the file system. This tutorial is an overview of Java I/O and all the classes in the package. And explains how to handle file management in java.

File Management in Java

I/O Introduction

  • Data in files on your system is called persistent data because it persists after the program runs.
  • Files are created through streams in Java.
  • A stream is a linear, sequential flow of bytes of input or output data.
  • Streams are written to the file system to create files.
  • Streams can also be transferred over the Internet.
  • Three streams are created for us automatically:
    System.out - standard output stream - standard input stream
    System.err - standard error

Basic requirements to create files in java.

Text or binary

What's your format for storing or transmitting data? Will you be using text or binary data?

If you use binary data, such as integers or doubles, then use the InputStream and OutputStream classes.

If you are using text data, then the Reader and Writer classes are right.

Random access

Do you want random access to records? Random access allows you to go anywhere within a file and be able to treat the file as if it were a collection of records.

The RandomAccessFile class permits random access. The data is stored in binary format. Using random access files improves performance and efficiency.

Object or non-object

Are you inputting or outputting the attributes of an object? If the data itself is an object, then use the ObjectInputStream and ObjectOutputStream classes.

Sources and sinks for data

What is the source of your data? What will be consuming your output data, that is, acting as a sink? You can input or output your data in a number of ways: sockets, files, strings, and arrays of characters.

Any of these can be a source for an InputStream or Reader or a sink for an OutputStream or Writer.


Do you need filtering for your data? There are a couple ways to filter data.

Buffering is one filtering method. Instead of going back to the operating system for each byte, you can use an object to provide a buffer.

Checksumming is another filtering method. As you are reading or writing a stream, you might want to compute a checksum on it. A checksum is a value you can use later on to make sure the stream was transmitted properly.

Storing data records

A data record is a collection of more than one element, such as names or addresses. There are three ways to store data records:

  1. Use delimited records, such as a mail-merge style record, to store values. On output, data values are converted to strings separated by delimiters such as the tab character, and ending with a new-line character. To read the data back into Java code, the entire line is read and then broken up using the StringTokenizer class.
  2. Use fixed size records to store data records. Use the RandomAccessFile class to store one or more records. Use the seek() method to find a particular record. If you choose this option to store data records, you must ensure strings are set to a fixed maximum size.
  3. Alternatively, you can use variable length records if you use an auxiliary file to store the lengths of each record. Use object streams to store data records. If object streams are used, no skipping around is permitted, and all objects are written to a file in a sequential manner.