File input and output in Java Java 18.11.2013

Java offers many classes for performing file input and output. These can be categorized as text I/O classes and binary I/O classes.

A program that needs to read data from some source needs an input stream or Reader. A program that needs to write data to some destination needs an output stream or writer.

File object encapsulates the properties of a file or a path but does not contain the methods for reading/writing data from/to a file. In order to perform I/O, you need to create objects using appropriate Java I/O classes.

Following table summarize classes for input and output (source).

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

Byte based I/O classes

The abstract InputStream is the root class for reading binary data and the abstract OutputStream is the root class for writing binary data.


FileInputStream/FileOutputStream is for reading/writing bytes from/to files. All the methods in these classes are inherited from InputStream and OutputStream.

Almost all the methods in the I/O classes throw

Following code show example of FileOutputStream and FileInputStream

public class TestFile {
    public static void main(String[] args) throws IOException {
        // write
        FileOutputStream output = new FileOutputStream("file.txt");
        for (int i = 1; i <= 10; i++) {

        // read 
        FileInputStream input = new FileInputStream("file.txt");
        int value;
        while ((value = ) != -1) {

DataInputStream reads bytes from the stream and converts them into appropriate primitive type values or strings. DataOutputStream converts primitive type values or strings into bytes and outputs the bytes to the stream. DataInputStream and DataOutputStream read and write Java primitive type values and strings in a machine-independent fashion, thereby enabling you to write a data file on one machine and read it on another machine that has a different operating system or file structure.

DataInputStream/DataOutputStream enables you to perform I/O for primitive type values and strings. ObjectInputStream/ObjectOutputStream enables you to perform I/O for objects in addition to primitive type values and strings. Since ObjectInputStream/ObjectOutputStream contains all the functions of DataInputStream/DataOutputStream, you can replace DataInputStream/DataOutputStream completely with ObjectInputStream/ObjectOutputStream.

BufferedInputStream/BufferedOutputStream can be used to speed up input and output by reducing the number of disk reads and writes. Using BufferedInputStream, the whole block of data on the disk is read into the buffer in the memory once. The individual data are then delivered to your program from the buffer. Using BufferedOutputStream, the individual data are first written to the buffer in the memory. When the buffer is full, all data in the buffer is written to the disk once,

Text based I/O classes

In Java, there are many ways to read or write a file.

Simple function for read whole content of file by Files.readAllBytes.

//import java.nio.file.Files;
//import java.nio.file.Path;
//import java.nio.file.Paths;

public static String getFileContent(String fname) {
    Path path = Paths.get(fname);
    String content = "";
    byte[] bytes;
    try {
        bytes = Files.readAllBytes(path);
        content = new String(bytes, "UTF-8");
    } catch (IOException e) {
    return content;

Examples of read file

Examples of write file

Useful links