CH 4. Exploring Java - Io
CH 4. Exploring Java - Io
CH 4. Exploring Java - Io
The java.io package consists of input and output streams used to read and write data to files
or other input and output sources.
Input Streams.
Output Streams.
Error Streams.
Java supports three streams that are automatically attached with the console.
Input Streams
As we know input source consists of data that needs to be read in order to extract information
from it. Input Streams help us to read data from the input source. It is an abstract class that
provides a programming interface for all input streams.
Input streams are opened implicitly as soon as it is created. To close the input stream, we use
a close() method on the source object.
Output Streams
The output of the executed program has to be stored in a file for further use. Output streams
help us to write data to a output source(may be file). Similarly like input streams output
streams are also abstract classes that provides a programming interface for all output streams.
The output stream is opened as soon as it is created and explicitly closed by using the close()
method.
Error Streams
Error streams are the same as output streams. In some ide’s error is displayed in different
colors (other than the color of output color). It gives output on the console the same as output
streams.
In day-to-day work, we do not enter the input into the programs manually. Also, the result of
the program needs to be stored somewhere for further use.
So, IO streams in Java provide us with input and output streams that help us to extract data
from the files and write the data into the files. Normally, we can create, delete, and edit files
using Java.io.
In short, all the file manipulation is done using Java IO streams. Java IO streams also handle
user input functionality.
Input Stream
It is an abstract superclass of the java.io package and is used to read the data from an input
source. In other words, reading data from files or from a keyboard, etc. We can create an
object of the input stream class using the new keyword. The input stream class has several
types of constructors.
The following code takes the file name as a string, to read the data stored in the file.
InputStream Hierarchy
Useful methods of InputStream
The method above helps to return the data of the next byte in the input stream. The value
returned is between 0 to 255. If no byte is read, the code returns -1, which indicates the end of
the file.
The method above returns the number of bytes that can be read from the input stream.
It marks the current position in the input stream. The readlimit argument tells the input stream
to read that many bytes to read before the mark position gets invalid.
It tells whether the mark() and reset() method is supported in a particular input stream. It
returns true if the mark and reset methods are supported by the particular input stream or else
return false.
The method above reads the bytes from the input stream and stores every byte in the buffer
array. It returns the total number of bytes stored in the buffer array. If there is no byte in
the input stream, it returns -1 as the stream is at the end of the file.
It reads up to len bytes of data from the input stream. It returns the total number of bytes
stored in the buffer. Here the “off” is start offset in buffer array b where the data is written,
and the “len” represents the maximum number of bytes to read.
It repositions the stream to the last called mark position. The reset method does nothing for
input stream class except throwing an exception.
Examples
1. In the below example, we will use FileInputStream class to read the input file: input.txt.
Scaler Topics
From InterviewBit
Code:
import java.io.*;
class Main {
public static void main(String[] args) throws IOException {
try {
// loading a file into f variable
FileInputStream f = new FileInputStream("input.txt");
// initializing x to 0
int x = 0;
// while loop untill the end of the file.
while ((x = f.read()) != -1) {
// printing the character
System.out.print((char) x);
}
// closing a file
f.close();
} catch (Exception e) {
// printing exception
System.out.println(e);
}
}
}
Output:
Scaler Topics
From InterviewBit
2. In the below example, we will use BufferedInputStream class to read the file.
Code:
import java.io.*;
class Main {
int x = 0;
// while loop untill the end of the file.
while ((x = f2.read()) != -1) {
// printing the character
System.out.print((char) x);
}
System.out.println();
// closing a file
f2.close();
} catch (Exception e) {
// printing exception
System.out.println(e);
}
}
}
Output:
Available bytes: 31
Scaler Topics
From InterviewBit
3. In the below example we will use ByteArrayInputStream class to read the file.
Code:
import java.io.*;
class Main {
int x = 0;
String S = "";
// while loop untill the end of the file.
while ((x = f1.read()) != -1) {
// printing the character
S = S + (char) x;
}
// closing a input stream
f1.close();
x = b1.read();
while (x != -1) {
System.out.print((char) x);
x = b1.read();
}
System.out.println();
// close the input stream
b1.close();
} catch (Exception e) {
// printing exception
System.out.println(e);
}
}
}
Output:
Scaler Topics
From InterviewBit
Output Stream
It is an abstract superclass of the java.io package and writes data to an output resource. In
other words, writing the data into the files. We can create an object of the output stream class
using the new keyword. The output stream class has several types of constructors.
OutputStream Hierarchy
This method closes the current output stream and releases any system resources associated
with it. The closed stream cannot be reopened and operations cannot be performed within it.
This method writes the b.length bytes from the specified byte array to the output stream.
It writes upto len bytes of data to the output stream. Here the “off” is the start offset in buffer
array b, and the “len” represents the maximum number of bytes to be written in the output
stream.
The method above writes the specific bytes to the output stream. It does not return a value.
Some methods that are inherited from class java.lang.Object. These methods are used for
both input stream and output stream purposes.
Example: clone, equals, finalise, getclass, hashCode, notify, notifyAll, toString, wait.
Examples
1. In the below example we will use FileOutputStream class to read the file.
Code:
import java.io.*;
class Main {
Scaler Topics
2. In the below example we will use BufferedOutputStream class to read the file.
Code:
import java.io.*;
class Main {
// declaring a f1 as BufferedOutputStream
BufferedOutputStream f2 = new BufferedOutputStream(f1);
Output:
Scaler Topics
3. In the below example we will use ByteArrayOutputStream class to read the file.
Code:
import java.io.*;
class Main {
// declaring ByteArrayOutputStream
ByteArrayOutputStream b1 = new ByteArrayOutputStream();
// closing a file
b1.close();
} catch (Exception e) {
// printing exception
System.out.println(e);
}
}
}
Output:
Scaler Topics