Tutorials     About     RSS
Tech and Media Labs

Java IO Tutorial

Jakob Jenkov
Last update: 2020-03-15

Java IO is an API that comes with Java which is targeted at reading and writing data (input and output). Most applications need to process some input and produce some output based on that input. For instance, read data from a file or over network, and write to a file or write a response back over the network.

The Java IO API is located in the Java IO package (java.io). If you look at the Java IO classes in the java.io package the vast amount of choices can be rather confusing. What is the purpose of all these classes? Which one should you choose for a given task? How do you create your own classes to plugin? etc.

The purpose of this tutorial is to try to give you an overview of how all these classes are grouped, and the purpose behind them, so you don't have to wonder whether you chose the right class, or whether a class already exists for your purpose.

Java IO Across Java Versions

The Java IO API has remained reasonably stable across the many Java versions that has come since it was first created. However, small things have changed, like how to close an InputStream or OutputStream best using the try-with-resources constructs. This Java IO tutorial has been updated in many places to reflect these changes whenever they have occurred.

Java IO Overview

A good place to start learning about Java IO is the Java IO Overview tutorial. That tutorial gives you a quick overview of the central concepts in the Java IO API, and an overview of all the central classes in the Java IO API.

The Scope of the Java IO (java.io) Package

The java.io package doesn't actually address all types of input and output. For instance, input from and output to a GUI or web page is not covered in the Java IO package. Those types of input are covered elsewhere, for instance by the JFC classes in the Swing project, or the Servlet and HTTP packages in the Java Enterprise Edition.

The Java IO package is primarily focused on input and output to files, network streams, internal memory buffers etc. However, the Java IO package does not contain classes to open network sockets which are necessary for network communication. For that purpose you need to use the Java Networking API. Once you have opened a socket (network connection) though, you read and write data to and from it via Java IO's InputStream and OutputStream classes.

Java NIO - The Alternative IO API

Java also contains another IO API called Java NIO. It contains classes that does much of the same as the Java IO and Java Networking APIs, but Java NIO can work in non-blocking mode. Non-blocking IO can in some situations give a big performance boost over blocking IO.

More Java IO Tools, Tips etc.

The tutorial trail called Java How To's and Utilities also contain a few Java IO utilities - e.g. replacing strings in streams, iterating streams using buffers etc.

Java IO Class Overview Table

Here is a table listing most (if not all) Java IO classes divided by input, output, being byte based or character based, and any more specific purpose they may be addressing, like buffering, parsing etc.

  Byte Based Character Based
  Input Output Input Output
Basic InputStream OutputStream Reader
InputStreamReader
Writer
OutputStreamWriter
Arrays ByteArrayInputStream ByteArrayOutputStream CharArrayReader CharArrayWriter
Files FileInputStream
RandomAccessFile
FileOutputStream
RandomAccessFile
FileReader FileWriter
Pipes PipedInputStream PipedOutputStream PipedReader PipedWriter
Buffering BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter
Filtering FilterInputStream FilterOutputStream FilterReader FilterWriter
Parsing PushbackInputStream
StreamTokenizer
  PushbackReader
LineNumberReader
 
Strings     StringReader StringWriter
Data DataInputStream DataOutputStream  
Data - Formatted   PrintStream   PrintWriter
Objects ObjectInputStream ObjectOutputStream    
Utilities SequenceInputStream
     

Jakob Jenkov

Featured Videos

















Sponsored Ads

Maildroppa - Smart Email Marketing Solution
Close TOC

All Trails

Trail TOC

Page TOC

Previous

Next