Lecture-25-Introduction to File IO
Lecture-25-Introduction to File IO
4-3
Introduction
• File Handling
– Way to handle the files stored on the hard disk
– So that we can, read and write the data in file
– File handling is a way to interact with external files—to either save
data or retrieve it.
• Java provides java.io package
– Contain all class that help in performing input and output operations
on files
• The main purpose of file handling is to:
4-5
Java provides java.io package
• Java provides java.io package
"Contain all class that help in performing input and output operations on files"
java.io is a built-in Java package that provides classes for:
• File creation
• File reading and writing
• Stream-based input/output (both byte and character streams)
Common classes include:
• File, FileReader, FileWriter
• BufferedReader, BufferedWriter
• FileInputStream, FileOutputStream
• ObjectInputStream, ObjectOutputStream (for saving objects)
(java.io) to provide reusable, ready-to-use classes for performing file operations,
making it easier for developers to store and retrieve data persistently.
4-6
• Stream
Java Streams – A stream is a sequence of data.
– In Java, a stream is composed of bytes
called a stream
–
In java.io package all classes are divided into two Stream represent Source( which generate
type of streams the data in form of streams) and
1- Character Stream(for reading/writing text Destination (which consume or read data
(characters). available on stream)
2- Byte Stream(for reading/writing binary data
(bytes).
What is a Byte Stream?
• It deals with binary data — the raw sequence of 0's and 1's.
• This kind of data could be audio, video, or text files.
• It reads or writes data one byte at a time (1 byte = 8 bits).
Why Use Byte Streams?
• Useful for handling non-text data like images, videos, or any file
with binary content.
4-8
Byte Stream
(100111010111101011101)
• Raw sequence of 0's and 1’s to represent
data BufferedInputStream
• The data belong to either audio, video or BufferedoutputStream
text. ByteArrayInputStream
• Just read/write data in form of 8bits(byte ByteArrayOutputStream
by byte) DataInputStream
• Java classes that handle Byte Stream and DataOutputStream
FileInputStream
They come from three main sources:
FileOutputStream
– java.io.InputStream PrintStream
– java.io.OutputStream
– java.io.RandomAccessFile
Examples of Byte Stream Classes
• BufferedInputStream / BufferedOutputStream: For fast reading/writing
using a buffer.
• ByteArrayInputStream / ByteArrayOutputStream: Works with byte
arrays.
• DataInputStream / DataOutputStream: Allows reading/writing Java
primitive data types (like int, double).
• FileInputStream / FileOutputStream: For reading/writing raw bytes
from/to files.
• PrintStream: Used to print data in a byte stream format (often used with
System.out).
4-10
What is a Character Stream?
• A Character Stream is used to read and write text data
(characters) instead of binary data (like in Byte Stream).
• It is based on Byte Stream but works with 16 bits at a time
(because Java characters are 16-bit, using Unicode).
• Character Stream works only with character data (not binary
files like images or videos).
• It uses Unicode encoding, which can represent characters from
many languages.
• It is ideal for reading/writing text files.
4-11
Character Stream
(11110110 11010101 101010101)
• Character Stream is based on Byte
stream, and bunch of 16 bits are picked BufferedReader
as character BufferedWriter
CharArrayReader
• Data must be characters CharArrayWriter
• Characters are encoded using a character FileReader
encoding scheme such as Unicode FileWriter
InputStreamReader
• The classes that handle Character Stream OutputStreamWriter
and which are descendent of PrintWriter
– java.io.Reader StringReader
– java.io.Writer class StringWriter
Main Java Classes for Character Stream:
• BufferedReader / BufferedWriter: Fast reading/writing using a buffer.
• CharArrayReader / CharArrayWriter: Works with character arrays.
• FileReader / FileWriter: Reads/writes from text files.
• InputStreamReader / OutputStreamWriter: Converts byte streams into
character streams and vice versa.
• PrintWriter: Prints formatted text.
• StringReader / StringWriter: Works with strings as input/output.
4-13
File Class
The File class is used to work with files and folders (directories).It helps you check properties of a
file or directory (like if it exists or not).It also allows you to rename or delete files/folders.
while(input.hasNext()){
String fName = input.next();
String mName = input.next();
String lName = input.next();
int age = input.nextInt();
System.out.println(fName+" "+mName+" "+lName+" "+age);
}
input.close();
}
catch(IOException e){