Java File Streams
File streams in Java are used for reading from and writing to files. They are part of the java.io
package and provide a way to handle input and output of byte streams and character streams.
1. Byte Streams
Byte streams are used to perform input and output of 8-bit bytes. The main classes are FileInputStream
and FileOutputStream
.
Example: Reading Bytes from a File
import java.io.FileInputStream;
import java.io.IOException;
public class ByteStreamReadExample {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("input.bin");
int data;
while ((data = fis.read()) != -1) {
// Process byte data
System.out.print((char) data);
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Example: Writing Bytes to a File
import java.io.FileOutputStream;
import java.io.IOException;
public class ByteStreamWriteExample {
public static void main(String[] args) {
String data = "Hello, byte stream!";
try {
FileOutputStream fos = new FileOutputStream("output.bin");
fos.write(data.getBytes());
fos.close();
System.out.println("Data written successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. Character Streams
Character streams are designed for handling 16-bit Unicode characters. The main classes are FileReader
and FileWriter
.
Example: Reading Characters from a File
import java.io.FileReader;
import java.io.IOException;
public class CharStreamReadExample {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("input.txt");
int data;
while ((data = fr.read()) != -1) {
// Process character data
System.out.print((char) data);
}
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Example: Writing Characters to a File
import java.io.FileWriter;
import java.io.IOException;
public class CharStreamWriteExample {
public static void main(String[] args) {
String data = "Hello, character stream!";
try {
FileWriter fw = new FileWriter("output.txt");
fw.write(data);
fw.close();
System.out.println("Data written successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. Buffered Streams
Buffered streams can improve I/O performance by reducing the number of reads and writes that need to be performed. Classes include BufferedInputStream
, BufferedOutputStream
, BufferedReader
, and BufferedWriter
.
Example: Using BufferedReader
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. Key Takeaways
- File streams provide mechanisms to read and write data to files.
- Use byte streams for binary data and character streams for text data.
- Buffered streams can improve I/O efficiency.
- Always close streams to free system resources.
- Handle exceptions to prevent crashes and data corruption.