Exploring Java IO
Exploring Java IO
import java.io.*;
Here we've used * operator to import all classes from java.io package and now any class can be used
in the program. In case of specific class, for example InputStream, we can import a class using
following syntax:
import java.io.InputStream;
Powered by:
Why java.io Package is used in Java Programs
Java.io package classes contains stream/reader/writer classes to read and write to varied source
using a uniform pattern. Following list shows some of the categories of classes of java.io package.
Page 2 of 12
Input Streams − Input Streams classes like ByteArrayInputStream, FileInputStream etc. helps
to read data from sources like byte array, files as a stream of data and we can read the
complete content using buffering as well.
Reader −Reader classes are char oriented and are specialized to read text based content.
Writer − Similarly Writer classes are specialized to write text based content to any
destination.
Console −The Console class provides methods to access the character-based console
device, if any, associated with the current Java virtual machine.
DataInputStream −The DataInputStream class lets an application read primitive Java data
types from an underlying input stream in a machine-independent way.
DataOutputStream −The DataOutputStream class lets an application write primitive Java
data types to an output stream in a portable way. An application can then use a data input
stream to read the data back in.
File −Java File class represents the files and directory pathnames in an abstract manner. This
class is used for creation of files and directories, file searching, file deletion, etc. The File
object represents the actual file/directory on the disk.
FileDescriptor −The FileDescriptor class instances serve as an opaque handle to the
underlying machine-specific structure representing an open file, an open socket, or another
source or sink of bytes
FileInputStream −The FileInputStream class obtains input bytes from a file in a file system.
What files are available depends on the host environment.
FileOutputStream −The FileOutputStream class is an output stream for writing data to a File
or to a FileDescriptor.
FileReader −The FileReader class is a convenience class for reading character files.
FileWriter −The FileWriter class is a convenience class for writing character files.
FilterInputStream −The FilterInputStream class contains some other input stream, which it
uses as its basic source of data, possibly transforming the data along the way or providing
additional functionality.
FilterOutputStream −The FilterOutputStream class is the superclass of all classes that filter
output streams.
FilterReader −The FilterReader class is for reading filtered character streams.
ObjectOutputStream −The ObjectOutputStream class writes primitive data types and graphs
of Java objects to an OutputStream.The objects can be read (reconstituted) using an
ObjectInputStream.
io - ObjectOutputStream.PutField −The ObjectOutputStream.PutField class provide
programmatic access to the persistent fields to be written to ObjectOutput.
ObjectStreamClass −The ObjectStreamClass class is Serialization's descriptor for classes. It
contains the name and serialVersionUID of the class. The ObjectStreamClass for a specific
class loaded in this Java VM can be found/created using the lookup method.
PrintStream −The PrintStream class adds functionality to another output stream, the ability to
print representations of various data values conveniently.
Page 5 of 12
Reader −The Reader class is a abstract class for reading character streams.
SequenceInputStream −The SequenceInputStream class represents the logical
concatenation of other input streams. It starts out with an ordered collection of input streams
and reads from the first one until end of file is reached, whereupon it reads from the second
one, and so on, until end of file is reached on the last of the contained input streams.
SerializablePermission −The SerializablePermission class is for Serializable permissions. A
SerializablePermission contains a name (also referred to as a "target name") but no actions
list; you either have the named permission or you don't.The target name is the name of the
Serializable permission.
StreamTokenizer −The StreamTokenizer class takes an input stream and parses it into
"tokens", allowing the tokens to be read one at a time. The stream tokenizer can recognize
identifiers, numbers, quoted strings, and various comment styles.
StringWriter −The StringWriter class is a character stream that collects its output in a string
buffer, which can then be used to construct a string.Closing a StringWriter has no effect. The
methods in this class can be called after the stream has been closed without generating an
IOException.
Writer −The Writer class is a abstract class for writing to character streams.
Example of java.io.ByteArrayInputStream
The following program illustrates several of the methods supported by ByteArrayInputStream −
Page 6 of 12
Open Compiler
package com.tutorialspoint;
import java.io.ByteArrayInputStream;
import java.io.IOException;
try {
// create new byte array input stream
bais = new ByteArrayInputStream(buf);
int b =0;
// print
System.out.println("byte :"+b+"; char : "+ c);
}
System.out.print(bais.read()+" Reached the end");
} catch(Exception e) {
// if I/O error occurs
e.printStackTrace();
} finally {
if(bais!=null)
bais.close();
}
}
Powered by:
}
Page 7 of 12
Output
Let us compile and run the above program, this will produce the following result −
Example of java.io.ByteArrayOutputStream
The following program illustrates several of the methods supported by ByteArrayOutputStream −
Open Compiler
package com.tutorialspoint;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
try {
// create new ByteArrayOutputStream
baos = new ByteArrayOutputStream();
// assign integer
int b = 87;
Powered by:
// write to stream
bos.write(b);
Page 8 of 12
} catch(IOException e) {
// if I/O error occurs.
e.printStackTrace();
} finally {
// releases any system resources associated with the stream
if(baos!=null)
baos.close();
if(bos!=null)
bos.close();
}
}
}
Output
Let us compile and run the above program, this will produce the following result −
87
Example of java.io.PrintWriter
The following program illustrates several of the methods supported by PrintWriter −
Open Compiler
package com.tutorialspoint;
import java.io.*;
Powered by:
try {
// create a new writer
PrintWriter pw = new PrintWriter(System.out);
// print string
pw.print(s);
Output
Let us compile and run the above program, this will produce the following result −
Hello world.
This is an example.
Powered by: