Section 2
Section 2
Java IO Streams
Learning Objectives:
In this lesson, we will explore how to work with Java IO Stream classes. By the end of the lesson, learners will be able to:
Describe Java IO Stream.
Describe byte-based Stream and Character-based Stream.
Explain Java IO - Streams Super Classes and classes hierarchy.
Demonstrate how to use the Java IO Streams classes.
Table of Contents
Topic 1a: Java I/O - Stream
Topic 1b: Java I/O - Stream Classes - Overview
Topic 2: Java IO - Streams Super Classes - Overview
Topic 3: File Handling Using Java IO - Character-Based Stream Classes
Topic 3a: Introduction to Java IO - Character-Based Streams
Topic 3b: Overview of FileReader() Class
Topic 3c: Overview of FileWriter() Class
Topic 3d: Overview of PrintWriter() Class
Topic 4: File Handling using Java I/O Byte-Based Stream Classes
Topic 4a: Introduction to Java IO - Byte-Based Streams
Topic 4b: Java IO - Byte-Based Stream Classes
Topic 4e: Overview of Byte-Based Stream Classes
Topic 4f: Classes List for Byte-Based and Character-Based Streams
Topic 1
Java I/O - Stream
Topic 1a: Java I/O - Stream
Java I/O Streams read from a stream or write to an endless flow of data. You stream a stream is a conceptually can either read from a core concept in Java IO. A stream is connected to a data source or data destination. Streams in Java IO can be either byte-based (reading and writing bytes) or character-based (reading and writing characters). Data that flows through a given stream flows in one direction only — input or output.
Topic 1b: Java I/O - Stream Classes - Overview
The java.io
package contains the Java I/O stream classes. These classes are divided by input and output byte-based Stream and Character-based Stream. These two abstract classes have several concrete classes that handle various devices such as disk files and network connections, etc.
InputStream − The InputStream is used to read data byte-by-byte from a source file.
OutputStream − The OutputStream is used for writing data to a file byte-by-byte to a destination.
For Character-Based Stream: Character-based stream is also defined by using two abstract classes at the top of the hierarchy: Reader and Writer. These two abstract classes have several concrete classes that handle Unicode characters.
For Byte-Based Stream: Based stream is defined by using two abstract classes at the top of the hierarchy: InputStream and OutputStream.
Topic 2
Java I/O - Streams Super Classes
Topic 2: Java IO - Streams Super Classes - Overview
For byte-based Stream, the Super classes are InputStream and OutputStream.
For Character-based Stream the Super classes are Reader and Writer.
A program that needs to read data from some source needs an InputStream or a Reader, both of which are superclasses. A program that needs to write data to some destination needs an OutputStream or a Writer, both of which are superclasses.
Topic 3
File Handling Using Java IO - Character-Based Stream Classes
Topic 3a: Introduction to Java IO - Character-Based Streams
The Character-Based Streams can read/write characters that are Unicode-based text data. I/O with character-based streams is no more complicated than I/O with byte streams. Input and output performed with stream classes automatically translates to and from the local character set. A program that uses character streams in place of byte streams automatically adapts to the local character set and is ready for internationalization — all without extra effort by the programmer.
Topic 3b: Overview of FileReader() Class
FileReader
class is used to read data from the file, and inherits from the InputStreamReader
class.
Topic 3c: Overview of FileWriter() Class
FileWriter
Class is used to write character-oriented data to a file. It is a character-oriented class, which is used for file handling in Java.
Topic 3d: Overview of PrintWriter() Class
PrintWriter
class gives Prints formatted representations of objects to a text-output stream. It implements all of the print methods found in System.out.println(PrintStream class)
.
Topic 4
File Handling Using Java IO - Byte-Based Stream Classes
Topic 4a: Introduction to Java IO - Byte-Based Streams
A Byte-Based Streams accesses the file byte-by-byte. Java programs use byte-based streams to perform input and output of 8-bit bytes. It is suitable for any kind of file; however, not quite appropriate for text files.
Topic 4b: Byte-Based Stream Classes Hierarchy
Byte-Based Stream classes are used to perform reading and writing of 8-bit bytes. Java further divides byte stream classes into two classes: InputStream class and OutputStream class.
Topic 4e: Overview of Byte-Based Stream Classes
FileInputStream() - A built-in class in Java that allows reading data from a file. This class has implemented based on the byte-based stream.
FileOutputStream() - A built-in class in Java that allows writing data to a file. This class has implemented based on the byte-based stream.
FileInputStream() Class
Streaming Through the File
Create a simple file named sourcefile.txt
and write the code below:
The read()
method return type is int, although it returns a byte value. It returns -1 if the end of the file is reached, indicating that there are no more bytes to read. To read all bytes in a Java InputStream, you must keep reading until the value -1 is returned. This value means that there are no more bytes to read from the InputStream.
import java.io.*;
public class FileInputStreamExample {
public static void main(String args[]) throws IOException {
FileInputStream fin = new FileInputStream("C:/Downloads/testingFile.txt");
// System.out.println(fin.read());
int i = 0;
while((i = fin.read()) != -1) {
System.out.print((char)i);
}
fin.close();
}
}
FileInputStream and FileOutputStream Classes
In this example, we will read the data from a file and write the same data to another file using FileInputStream
and FileOutputStream
classes. The write()
method takes an int, which contains the byte value of the byte to write, and writes the single byte to the file output stream.
import java.io.*;
public class ExampleBytebasedStream {
public static void main(String[] args)throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("C:/Users/Downloads/testingFile.txt");
out = new FileOutputStream("C:/Users/Downloads/sampleOutput4.txt");
int c;
while ((c = in.read()) != -1) { // read byte by byte
out.write(c); // write byte by byte
}
System.out.println("Reading and Writing has been success!!");
} catch(Exception e){
System.out.println(e);
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
Classes List for Byte-Based and Character-Based Streams
Where we can utilize all of these classes:
Byte Based Classes
Basic
InputStream
OutputStream
Arrays
ByteArrayInputStream
ByteArrayOutputStream
Files
FileInputStream
FileOutputStream
Buffering
BufferedInputStream
BufferedOutputStream
Character Based Classes
Basic
Reader
Writer
Arrays
CharArrayReader
CharArrayWriter
Files
FileReader
FileWriter
Buffering
BufferedReader
BufferedWriter
Filtering
Filtering
FilterInputStream
FilterOutputStream
Parsing
FilterReader
FilterWriter
Data
PushbackInputStream
PushbackOutputStream
Data - Formatted
Data - Formatted
DataInputStream
DataOutputStream
Objects
ObjectInputStream
ObjectOutputStream
Utilities
SequenceInputStream
PrintStream
PrintWriter
Strings
StringReader
StringWriter
Data
StreamTokenizer
LineNumberReader
Knowledge Check
Java has two types of streams: Character Streams and Byte Streams. Why?
What is the difference between the two types of streams?
Can data flow through a given stream in both directions?
Last updated