Java I/O Streams
In Java, streams are the sequence of data that are read from the source and written to the destination.
An input stream is used to read data from the source. And, an output stream is used to write data to the destination.
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
For example, in our first Hello World example, we have used System.out to print a string. Here, the System.out is a type of output stream.
Similarly, there are input streams to take input.
What is Byte Stream in Java?
Byte streams are used to perform input and output of 8-bit bytes. They are used to read bytes from the input stream and write bytes to the output stream. Mostly, they are used to read or write raw binary data.
In Java, the byte streams have a 3 phase mechanism:
Split- The input data source is split into a stream by a spliterator. Java Spliterator interface is an internal iterator that breaks the stream into smaller parts for traversing over them.
Apply- The elements in the stream are processed.
Combine- After the elements are processed, they are again combined together to create a single result.
FileInputStream- This class is used to read data from a file/source. The FileInputStream class has constructors which we can use to create an instance of the FileInputStream class.
What is a character stream in Java?
In Java, characters are stored using Unicode conventions. Character stream automatically allows us to read/write data character by character. For example, FileReader and FileWriter are character streams used to read from the source and write to the destination.
Reading console input
1.Using Buffered Reader Class
This is the Java classical method to take input, Introduced in JDK1.0. This method is used by wrapping the System.in (standard input stream)
2. Using Scanner Class
This is probably the most preferred method to take input. The main purpose of the Scanner class is to parse primitive types and strings using regular expressions
3. Using Console Class
It has been becoming a preferred way for reading user’s input from the command line.
Writing console output using print() and println() methods
The PrintStream is a bult-in class that provides two methods print() and println() to write console output. The print() and println() methods are the most widely used methods for console output.
Both print() and println() methods are used with System.out stream.
Reading and writing files
Reader, InputStreamReader, FileReader and BufferedReader
Reader is the abstract class for reading character streams. It implements the following fundamental methods:
read(): reads a single character.
read(char[]): reads an array of characters.
skip(long): skips some characters.
close(): closes the stream.
InputStreamReader is a bridge from byte streams to character streams. It converts bytes into characters using a specified charset. The charset can be default character encoding of the operating system, or can be specified explicitly when creating an InputStreamReader.
FileReader is a convenient class for reading text files using the default character encoding of the operating system.
BufferedReader reads text from a character stream with efficiency (characters are buffered to avoid frequently reading from the underlying stream) and provides a convenient method for reading a line of text readLine().
Writer, OutputStreamWriter, FileWriter and BufferedWriter
Writer is the abstract class for writing character streams. It implements the following fundamental methods:
write(int): writes a single character.
write(char[]): writes an array of characters.
write(String): writes a string.
close(): closes the stream.
OutputStreamWriter is a bridge from byte streams to character streams. Characters are encoded into bytes using a specified charset. The charset can be default character encoding of the operating system, or can be specified explicitly when creating an OutputStreamWriter.
FileWriter is a convenient class for writing text files using the default character encoding of the operating system.
BufferedWriter writes text to a character stream with efficiency (characters, arrays and strings are buffered to avoid frequently writing to the underlying stream) and provides a convenient method for writing a line separator: newLine().
Post a Comment
0 Comments