- Byte Streams: Handle data in raw binary format (8-bit bytes). Used for reading and writing binary data such as images or audio files.
- Character Streams: Handle data in 16-bit Unicode format. Used for reading and writing text files.
// Byte Stream Example
FileInputStream fis = new FileInputStream("input.txt");
int data;
while ((data = fis.read()) != -1) {
System.out.print((char) data);
}
fis.close();
// Character Stream Example
FileReader fr = new FileReader("input.txt");
int charData;
while ((charData = fr.read()) != -1) {
System.out.print((char) charData);
}
fr.close();- File: Represents file or directory paths.
- FileInputStream and FileOutputStream: For byte stream operations.
- FileReader and FileWriter: For character stream operations.
- BufferedReader and BufferedWriter: For efficient character stream operations.
- ObjectInputStream and ObjectOutputStream: For serialization and deserialization.
File file = new File("example.txt");
if (file.exists()) {
System.out.println("File exists: " + file.getName());
}You can use BufferedReader to read a file line by line.
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();These classes are used for object serialization and deserialization.
ObjectOutputStream: Writes objects to a file.ObjectInputStream: Reads objects from a file.
// Serialization
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
oos.writeObject(new String("Hello World"));
oos.close();
// Deserialization
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.dat"));
String message = (String) ois.readObject();
System.out.println(message);
ois.close();File locking prevents other processes from accessing a file concurrently. It can be achieved using the FileChannel class.
FileChannel channel = new RandomAccessFile("file.txt", "rw").getChannel();
FileLock lock = channel.lock();
try {
System.out.println("File is locked");
} finally {
lock.release();
channel.close();
}You can use Files.copy from java.nio.file package or FileInputStream and FileOutputStream.
// Using Files.copy
Files.copy(Paths.get("source.txt"), Paths.get("destination.txt"), StandardCopyOption.REPLACE_EXISTING);
// Using Streams
FileInputStream fis = new FileInputStream("source.txt");
FileOutputStream fos = new FileOutputStream("destination.txt");
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
fis.close();
fos.close();flush(): Ensures all data in the stream's buffer is written to the destination without closing the stream.close(): Releases resources and closes the stream.
BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
bw.write("Hello World");
bw.flush(); // Data written to file
bw.close(); // Stream closedRandom access files allow reading and writing at any position within a file. It is supported by the RandomAccessFile class.
RandomAccessFile raf = new RandomAccessFile("data.txt", "rw");
raf.seek(10); // Move to byte 10
raf.writeUTF("Hello");
raf.seek(0); // Move to the beginning
System.out.println(raf.readUTF());
raf.close();Buffered streams improve I/O performance by reducing the number of read and write operations. They use an internal buffer to batch operations.
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
br.close();
bw.close();These classes allow data to be transferred between threads using a pipe. They are useful for thread communication.
PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pos);
new Thread(() -> {
try {
pos.write("Hello from producer".getBytes());
pos.close();
} catch (IOException e) {
e.printStackTrace();
}
}).start();
new Thread(() -> {
try {
int data;
while ((data = pis.read()) != -1) {
System.out.print((char) data);
}
pis.close();
} catch (IOException e) {
e.printStackTrace();
}
}).start();