Govt. Exams
Entrance Exams
PrintWriter provides print() and printf() methods for formatted output. It is the best choice for formatted writing to files or streams.
InputStream and Reader are independent class hierarchies. InputStream is for byte streams and Reader is for character streams. They do not extend each other.
DataInputStream provides methods like readInt(), readDouble(), readUTF() to read primitive data types and strings from binary streams.
The default buffer size for BufferedInputStream is 8192 bytes (8 KB). This can be overridden by using the constructor BufferedInputStream(InputStream, int size).
FileOutputStream(String filename) by default overwrites existing file. Use FileOutputStream(File, boolean append) with true to append.
Try-with-resources (try-with-resources statement) automatically closes AutoCloseable resources, ensuring proper cleanup even if exceptions occur.
FileChannel provides non-blocking and efficient I/O operations, memory-mapped file access, and file locking capabilities compared to traditional streams.
available() returns an estimate of bytes that can be read without blocking. It doesn't return total file size.
Processing large files in chunks using BufferedInputStream with an appropriate buffer size (like 8KB-64KB) balances memory usage and I/O efficiency.
InputStreamReader is a bridge from byte streams to character streams and accepts any InputStream with optional charset. FileReader is specifically for file input with default charset.