Entrance Exams
Govt. Exams
Attempting to read from a closed stream throws IOException. The stream should be checked and reopened if needed, or proper resource management (try-with-resources) should be used.
BufferedReader with readLine() is the most efficient and convenient way to read files line by line. It provides buffering and a dedicated method for reading lines.
serialVersionUID is used to verify that sender and receiver have compatible versions of a Serializable class. If serialVersionUID doesn't match during deserialization, InvalidClassException is thrown.
NotSerializableException is thrown when attempting to serialize an object whose class does not implement Serializable or contains non-serializable fields.
read() reads and returns a single byte as an int (0-255 or -1 for EOF). read(byte[] b) reads bytes into an array and returns the number of bytes read.
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.
FileOutputStream fos = new FileOutputStream("test.txt", true);
What does the 'true' parameter indicate?
The second parameter 'true' in FileOutputStream constructor indicates append mode. If 'false' or omitted, the file is overwritten. With 'true', new data is appended to the existing file.
DataInputStream provides methods like readInt(), readDouble(), readUTF() to read primitive data types and strings from binary streams.
The flush() method forces any buffered output bytes to be written out. It does not close the stream, allowing further writes after flushing.