Java File I/O and NIO - Complete Study Notes
------------------------------------------------------------
1. Introduction
------------------------------------------------------------
I/O (Input/Output) deals with reading from and writing to data sources.
Java provides two APIs:
- File I/O ([Link]): Stream-based, simple, blocking.
- File NIO ([Link]): Channel-buffer-based, faster, supports non-blocking I/O.
------------------------------------------------------------
2. File I/O ([Link])
------------------------------------------------------------
Based on Streams.
Types:
- Byte Streams: FileInputStream, FileOutputStream
- Character Streams: FileReader, FileWriter
- Buffered Streams: BufferedReader, BufferedWriter
- Object Streams: ObjectInputStream, ObjectOutputStream
Example: Reading a file
------------------------------------------------------------
try (BufferedReader reader = new BufferedReader(new FileReader("[Link]"))) {
String line;
while ((line = [Link]()) != null) {
[Link](line);
}
} catch (IOException e) {
[Link]();
}
Example: Writing a file
------------------------------------------------------------
try (BufferedWriter writer = new BufferedWriter(new FileWriter("[Link]"))) {
[Link]("Hello Java I/O!");
} catch (IOException e) {
[Link]();
}
------------------------------------------------------------
3. File NIO ([Link] and [Link])
------------------------------------------------------------
NIO (New I/O) uses Buffers and Channels.
Core Classes:
- Path, Files, FileChannel, ByteBuffer, AsynchronousFileChannel
Example: Reading using NIO
------------------------------------------------------------
List<String> lines = [Link]([Link]("[Link]"));
[Link]([Link]::println);
Example: Writing using NIO
------------------------------------------------------------
[Link]([Link]("nio_output.txt"), [Link]("Line 1", "Line 2"),
[Link]);
Example: FileChannel
------------------------------------------------------------
FileInputStream fis = new FileInputStream("[Link]");
FileChannel channel = [Link]();
ByteBuffer buffer = [Link](1024);
while ([Link](buffer) > 0) {
[Link]();
while ([Link]()) {
[Link]((char) [Link]());
}
[Link]();
}
------------------------------------------------------------
4. Comparison: File I/O vs NIO
------------------------------------------------------------
| Feature | File I/O ([Link]) | NIO ([Link]) |
|----------------|---------------------------|-----------------------------|
| Type | Stream-based | Channel-buffer-based |
| Blocking | Yes | Non-blocking supported |
| Performance | Slower | Faster for large files |
| Introduced | Java 1.0 | Java 1.4 / 7 (NIO.2) |
------------------------------------------------------------
5. Mini Project: File Copier
------------------------------------------------------------
I/O Version:
InputStream in = new FileInputStream("[Link]");
OutputStream out = new FileOutputStream("dest_io.txt");
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = [Link](buffer)) != -1) {
[Link](buffer, 0, bytesRead);
}
NIO Version:
FileChannel src = [Link]([Link]("[Link]"), [Link]);
FileChannel dest = [Link]([Link]("dest_nio.txt"), [Link],
[Link]);
[Link](src, 0, [Link]());
------------------------------------------------------------
6. Summary
------------------------------------------------------------
- Use File I/O for simple, small files.
- Use NIO for large, fast, non-blocking operations.
- NIO is modern and efficient.
------------------------------------------------------------
7. Practice Questions
------------------------------------------------------------
1. Differentiate between File I/O and NIO in Java.
2. What are Channels and Buffers in NIO?
3. Write code to copy file contents using FileChannel.
4. Which classes are used for character stream handling?
5. Explain the role of the Path and Files classes in NIO.