Input Output Files Programming in Java
1. Introduction and Concept of Stream
In Java, a stream is a flow of data. It can be:
- Input stream - data flows into your program.
- Output stream - data flows out from your program.
Streams help us read from or write to various data sources like files, networks, memory, etc.
2. Stream Classes
Java provides abstract classes and their subclasses to handle streams.
Two main types:
- Byte Stream Classes - for handling binary data (images, videos).
- Character Stream Classes - for handling text data.
Each type has input and output classes.
3. Byte Stream Classes
Used to read and write raw binary data.
Base Classes:
- InputStream - superclass for reading bytes.
- OutputStream - superclass for writing bytes.
Common Subclasses:
- FileInputStream, FileOutputStream
- BufferedInputStream, BufferedOutputStream
- DataInputStream, DataOutputStream
- ObjectInputStream, ObjectOutputStream
4. Input Stream Classes
Used to read binary data from a file or source.
Example:
FileInputStream fin = new FileInputStream("data.txt");
int data = fin.read();
5. Output Stream Classes
Used to write binary data to a file or destination.
Example:
FileOutputStream fout = new FileOutputStream("data.txt");
fout.write(65);
6. Examples of File Handling Using Stream Classes
Reading and Writing Files:
FileInputStream fin = new FileInputStream("input.txt");
FileOutputStream fout = new FileOutputStream("output.txt");
int ch;
while((ch = fin.read()) != -1) { fout.write(ch); }
fin.close(); fout.close();
7. Character Stream Classes
Used to read/write characters instead of bytes.
Base Classes:
- Reader - abstract class to read characters.
- Writer - abstract class to write characters.
Subclasses:
- FileReader, FileWriter
- BufferedReader, BufferedWriter
- InputStreamReader, OutputStreamWriter
8. Using File I/O Classes
Use the File class to create, delete, and check file properties.
Example:
File f = new File("data.txt");
if(f.exists()) {
System.out.println("File size: " + f.length());
}
9. I/O Exceptions
IOException is a checked exception. You must handle it.
Example:
try {
FileReader fr = new FileReader("data.txt");
} catch (IOException e) {
e.printStackTrace();
}
10. Creation of Files
To create a file:
File f = new File("newfile.txt");
if(f.createNewFile()) {
System.out.println("File created!");
}
11. Reading and Writing Files
Text File Example:
FileWriter fw = new FileWriter("demo.txt");
fw.write("Hello World"); fw.close();
FileReader fr = new FileReader("demo.txt");
int c;
while((c = fr.read()) != -1) { System.out.print((char)c); }
fr.close();
12. Handling Primitive Data Types
Use DataOutputStream and DataInputStream to write and read primitive types.
Example:
DataOutputStream dout = new DataOutputStream(new FileOutputStream("data.dat"));
dout.writeInt(100);
dout.writeDouble(45.5);
dout.close();
DataInputStream din = new DataInputStream(new FileInputStream("data.dat"));
int x = din.readInt();
double y = din.readDouble();
din.close();
13. Examples of Handling Primitive Data Types
Store and retrieve multiple values of different types using DataStreams.
Hierarchy Diagram Summary:
Byte Stream:
InputStream -> FileInputStream, BufferedInputStream, DataInputStream
OutputStream -> FileOutputStream, BufferedOutputStream, DataOutputStream
Character Stream:
Reader -> FileReader, BufferedReader
Writer -> FileWriter, BufferedWriter