Java Input/Output: exploring java.
io, Java I/O classes and
interfaces, File, Stream classes, byte stream, character
stream, serialization.
1. File Handling using [Link]. File
🔹 Purpose: To check if a file exists and display its basic properties.
🔹 Code Example:
import [Link];
public class FileExample {
public static void main(String[] args) {
File file = new File("[Link]");
if ([Link]()) {
[Link]("File name: " + [Link]());
[Link]("Absolute path: " + [Link]());
[Link]("Writable: " + [Link]());
[Link]("Readable: " + [Link]());
[Link]("File size: " + [Link]() + " bytes");
} else {
[Link]("The file does not exist.");
}
}
}
🔹 Explanation:
- Creates a File object for '[Link]'.
- Checks if the file exists and prints its properties.
- Useful for file inspection before reading or writing.
2. Byte Stream using FileInputStream and FileOutputStream
🔹 Purpose: To copy contents of one file to another using byte streams.
🔹 Code Example:
import [Link];
import [Link];
import [Link];
public class ByteStreamExample {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("[Link]");
FileOutputStream out = new FileOutputStream("[Link]");
int content;
while ((content = [Link]()) != -1) {
[Link](content);
}
[Link]();
[Link]();
[Link]("File copied successfully.");
}
}
🔹 Explanation:
- Reads byte by byte from '[Link]' and writes to '[Link]'.
- Uses FileInputStream and FileOutputStream for binary data.
- Must close both streams after use.
3. Character Stream using FileReader and FileWriter
🔹 Purpose: To read and write text files using character streams.
🔹 Code Example:
import [Link];
import [Link];
import [Link];
public class CharacterStreamExample {
public static void main(String[] args) throws IOException {
FileReader reader = new FileReader("[Link]");
FileWriter writer = new FileWriter("[Link]");
int ch;
while ((ch = [Link]()) != -1) {
[Link](ch);
}
[Link]();
[Link]();
[Link]("Text copied successfully.");
}
}
🔹 Explanation:
- Character-based reading and writing, good for text files.
- Reads each character from '[Link]' and writes to '[Link]'.
- Reader and writer must be closed after use.
4. Serialization using ObjectOutputStream and ObjectInputStream
🔹 Purpose: To serialize and deserialize a simple object.
🔹 Code Example:
import [Link].*;
class Student implements Serializable {
int id;
String name;
Student(int id, String name) {
[Link] = id;
[Link] = name;
}
}
public class SerializationExample {
public static void main(String[] args) throws IOException,
ClassNotFoundException {
Student s = new Student(101, "Hassan");
ObjectOutputStream out = new ObjectOutputStream(new
FileOutputStream("[Link]"));
[Link](s);
[Link]();
ObjectInputStream in = new ObjectInputStream(new
FileInputStream("[Link]"));
Student deserialized = (Student) [Link]();
[Link]();
[Link]("Deserialized Student: " + [Link] + ",
" + [Link]);
}
}
🔹 Explanation:
- The Student class implements Serializable.
- ObjectOutputStream is used to write the object to a file.
- ObjectInputStream reads the object back and deserializes it.