1. Understanding Files and Directories
Files store data persistently on storage devices. Directories are used to organize files and may contain other directories, creating a hierarchical structure.
Example: Working with the File
Class
File file = new File("example.txt");
System.out.println("File exists: " + file.exists());
// Creating a new file
if (file.createNewFile()) {
System.out.println("File created successfully.");
}
// Deleting a file
if (file.delete()) {
System.out.println("File deleted successfully.");
}
2. Byte vs Character Streams
Java I/O supports two types of streams:
- Byte Streams: Work with binary data. Classes include
InputStream
andOutputStream
. - Character Streams: Work with text data. Classes include
Reader
andWriter
.
Byte Stream Example:
FileInputStream input = new FileInputStream("example.txt");
int data;
while ((data = input.read()) != -1) {
System.out.print((char) data);
}
input.close();
Character Stream Example:
FileReader reader = new FileReader("example.txt");
int data;
while ((data = reader.read()) != -1) {
System.out.print((char) data);
}
reader.close();
3. Low-Level vs High-Level Streams
Low-level streams operate directly on resources (e.g., files), while high-level streams filter or buffer data.
Example of Buffered Stream (High-Level):
BufferedReader bufferedReader = new BufferedReader(new FileReader("example.txt"));
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
4. Common Stream Classes
The following classes are essential for file and stream manipulation:
BufferedReader
/BufferedWriter
FileInputStream
/FileOutputStream
FileReader
/FileWriter
ObjectInputStream
/ObjectOutputStream
PrintWriter
5. Common Stream Operations
Input and output streams include methods for common operations:
close()
: Closes the stream.flush()
: Forces writing of buffered data.mark()
andreset()
: Mark a position in the stream and reset to it.skip()
: Skips a specified number of bytes.
Example:
FileInputStream input = new FileInputStream("example.txt");
if (input.markSupported()) {
input.mark(10); // Mark the current position
input.skip(5); // Skip 5 bytes
input.reset(); // Reset to the marked position
}
input.close();
6. Java Serialization
Serialization allows objects to be converted into a stream of bytes and saved to a file. Objects must implement the Serializable
interface.
Serialization Example:
class Person implements Serializable {
private String name;
private int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
// Writing an object
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("person.ser"));
out.writeObject(new Person("John", 30));
out.close();
Deserialization Example:
ObjectInputStream in = new ObjectInputStream(new FileInputStream("person.ser"));
Person p = (Person) in.readObject();
System.out.println("Name: " + p.name);
in.close();
7. User Interaction with the Console Class
The Console
class provides a way to read user input and display output securely.
Example:
Console console = System.console();
if (console != null) {
String userInput = console.readLine("Enter your name: ");
char[] password = console.readPassword("Enter your password: ");
System.out.println("Hello, " + userInput);
}
Conclusion
Java's java.io
package provides robust tools to work with files, directories, and streams. By understanding and using these tools effectively, developers can handle data persistence, manipulation, and user interaction with ease.
0 Comments