Java 4
Java 4
The java.io package contains nearly every class you might ever need to perform
The stream in the java.io package supports many data such as primitives, object,
Stream
A stream can be defined as a sequence of data. There are two kinds of Streams −
Byte Streams
Java byte streams are used to perform input and output of 8-bit bytes.
Though there are many classes related to byte streams but the most frequently
Following is an example which makes use of these two classes to copy an input file
Example
import java.io.*;
public class CopyFile {
FileInputStream in = null;
try {
in = new FileInputStream("input.txt");
int c;
out.write(c);
}finally {
if (in != null) {
in.close();
if (out != null) {
out.close();
creating output.txt file with the same content as we have in input.txt. So let's put the
$javac CopyFile.java
$java CopyFile
Character Streams
Java Byte streams are used to perform input and output of 8-bit bytes, whereas
Java Character streams are used to perform input and output for 16-bit unicode.
There are many classes related to character streams but the most frequently used
FileOutputStream but here the major difference is that FileReader reads two bytes
We can re-write the above example, which makes the use of these two classes to
Example
import java.io.*;
FileReader in = null;
try {
in = new FileReader("input.txt");
int c;
out.write(c);
}finally {
if (in != null) {
in.close();
if (out != null) {
out.close();
As a next step, compile the above program and execute it, which will result in
creating output.txt file with the same content as we have in input.txt. So let's put the
$javac CopyFile.java
$java CopyFile
Standard Streams
All the programming languages provide support for standard I/O where the user's
program can take input from a keyboard and then produce an output on the
computer screen.
If you are aware of C or C++ programming languages, then you must be aware of
three standard devices STDIN, STDOUT and STDERR. Similarly, Java provides the
Standard Input − This is used to feed the data to user's program and usually
Standard Output − This is used to output the data produced by the user's
program and usually a computer screen is used for standard output stream
Standard Error − This is used to output the error data produced by the user's
program and usually a computer screen is used for standard error stream
Example
import java.io.*;
char c;
do {
c = (char) cin.read();
System.out.print(c);
} while(c != 'q');
}finally {
if (cin != null) {
cin.close();
Let's keep the above code in ReadConsole.java file and try to compile and execute
it as shown in the following program. This program continues to read and output the
$javac ReadConsole.java
$java ReadConsole
e
q
The InputStream is used to read data from a source and the OutputStream is
FileInputStream
This stream is used for reading data from the files. Objects can be created using the
Following constructor takes a file object to create an input stream object to read the
Once you have InputStream object in hand, then there is a list of helper methods
This method closes the file output stream. Releases any system resources
This method cleans up the connection to the file. Ensures that the close
method of this file output stream is called when there are no more
This method reads the specified byte of data from the InputStream.
Returns an int. Returns the next byte of data and -1 will be returned if it's
This method reads r.length bytes from the input stream into an array.
Returns the total number of bytes read. If it is the end of the file, -1 will be
returned.
Gives the number of bytes that can be read from this file input stream.
Returns an int.
There are other important input streams available, for more detail you can refer to
ByteArrayInputStream
DataInputStream
FileOutputStream
FileOutputStream is used to create a file and write data into it. The stream would
Here are two constructors which can be used to create a FileOutputStream object.
Following constructor takes a file name as a string to create an input stream object
the file. First, we create a file object using File() method as follows −
Once you have OutputStream object in hand, then there is a list of helper methods,
This method closes the file output stream. Releases any system resources
This method cleans up the connection to the file. Ensures that the close
method of this file output stream is called when there are no more
Writes w.length bytes from the mentioned byte array to the OutputStream.
There are other important output streams available, for more detail you can refer to
ByteArrayOutputStream
DataOutputStream
Example
import java.io.*;
try {
os.close();
is.close();
} catch (IOException e) {
System.out.print("Exception");
The above code would create file test.txt and would write given numbers in binary
There are several other classes that we would be going through to get to know the
File Class
FileReader Class
FileWriter Class
Directories in Java
A directory is a File which can contain a list of other files and directories.
You use File object to create directories, to list down files available in a directory.
For complete detail, check a list of all the methods which you can call on File object
Creating Directories
There are two useful File utility methods, which can be used to create directories −
The mkdir( ) method creates a directory, returning true on success and false
on failure. Failure indicates that the path specified in the File object already
exists, or that the directory cannot be created because the entire path does
The mkdirs() method creates both a directory and all the parents of the
directory.
Example
import java.io.File;
d.mkdirs();
Note − Java automatically takes care of path separators on UNIX and Windows as
per conventions. If you use a forward slash (/) on a Windows version of Java, the
Listing Directories
You can use list( ) method provided by File object to list down all the files and
Example
import java.io.File;
String[] paths;
try {
paths = file.list();
for(String path:paths) {
System.out.println(path);
} catch (Exception e) {
e.printStackTrace();
}
}
This will produce the following result based on the directories and files available in
Output
test1.txt
test2.txt
ReadDir.java
ReadDir.class