Java Interview Questions

Previous Java Interview Questions Next

Input and Output (I/O) in Java

1. What is the difference between InputStream and OutputStream in Java?

InputStream is an abstract class that represents an input stream of bytes. It is used for reading data from a source. OutputStream is an abstract class that represents an output stream of bytes. It is used for writing data to a destination. Key implementations include FileInputStream, FileOutputStream, ByteArrayInputStream, and ByteArrayOutputStream.

2. What is the difference between Reader and Writer classes in Java?

Reader and Writer classes are character-based I/O classes, as opposed to byte-based InputStream and OutputStream. Reader is used for reading character streams, while Writer is used for writing character streams. They handle 16-bit Unicode characters, making them suitable for text data.

3. How do you read input from the console in Java?

There are several ways to read input from the console in Java:
1. Using Scanner class: Scanner scanner = new Scanner(System.in); String input = scanner.nextLine();
2. Using BufferedReader class: BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String input = reader.readLine();
3. Using Console class: Console console = System.console(); String input = console.readLine();

4. What is the purpose of the BufferedReader class?

BufferedReader reads text from a character-input stream, buffering characters to provide efficient reading of characters, arrays, and lines. It wraps around another Reader (like FileReader or InputStreamReader) to improve performance by reducing the number of I/O operations.

5. How do you write to a file in Java?

There are multiple ways to write to a file in Java:
1. Using FileWriter: FileWriter writer = new FileWriter("file.txt"); writer.write("Hello World"); writer.close();
2. Using BufferedWriter: BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt")); bw.write("Hello World"); bw.close();
3. Using PrintWriter: PrintWriter pw = new PrintWriter(new FileWriter("file.txt")); pw.println("Hello World"); pw.close();
4. Using Files.write() (Java 7+): Files.write(Paths.get("file.txt"), "Hello World".getBytes());

6. What is the difference between FileInputStream and FileReader?

FileInputStream is a byte stream class that reads raw bytes from a file, suitable for reading binary files like images or executables. FileReader is a character stream class that reads characters from a file, using the default character encoding, making it suitable for reading text files.

7. What is serialization in Java and how is it implemented?

Serialization is the process of converting an object into a byte stream for storage or transmission. To make a class serializable, it must implement the Serializable interface (a marker interface with no methods). Objects are serialized using ObjectOutputStream and deserialized using ObjectInputStream.

8. What is the transient keyword in Java?

The transient keyword is used in serialization. When applied to a field, it indicates that the field should not be serialized. During deserialization, transient fields are initialized with their default values (null for objects, 0 for numbers, false for boolean).

9. What is the purpose of the Scanner class in Java?

The Scanner class is a simple text scanner that can parse primitive types and strings using regular expressions. It breaks its input into tokens using a delimiter pattern (default is whitespace). It's commonly used for reading user input from the console and parsing files.

10. How do you read a file line by line in Java?

There are several ways to read a file line by line:
1. Using BufferedReader: BufferedReader br = new BufferedReader(new FileReader("file.txt")); String line; while ((line = br.readLine()) != null) { // process line }
2. Using Scanner: Scanner scanner = new Scanner(new File("file.txt")); while (scanner.hasNextLine()) { String line = scanner.nextLine(); }
3. Using Files.readAllLines() (Java 7+): List<String> lines = Files.readAllLines(Paths.get("file.txt"));
4. Using Files.lines() with Stream (Java 8+): Files.lines(Paths.get("file.txt")).forEach(line -> { // process line });

11. What is the difference between PrintStream and PrintWriter?

Both PrintStream and PrintWriter provide methods to print formatted representations of objects to a text output stream. The key difference is that PrintStream works with bytes and is used for writing to binary streams, while PrintWriter works with characters and is used for writing to character streams. System.out is a PrintStream object.

12. What is the purpose of the File class in Java?

The File class represents file and directory pathnames. It provides methods for:
- Creating, deleting, and renaming files
- Checking file properties (exists, isDirectory, isFile, canRead, canWrite)
- Getting file information (name, path, size, last modified)
- Listing directory contents
Note: In Java 7+, the Path interface and Files utility class provide more comprehensive file operations.

13. How do you copy a file in Java?

There are multiple ways to copy a file in Java:
1. Using Files.copy() (Java 7+): Files.copy(Paths.get("source.txt"), Paths.get("destination.txt"));
2. Using byte streams: Files.copy(new FileInputStream("source.txt"), new FileOutputStream("destination.txt"));
3. Using character streams: Files.copy(new FileReader("source.txt"), new FileWriter("destination.txt"));
4. Using FileChannel for large files: More efficient for large files.

14. What is the purpose of the DataInputStream and DataOutputStream classes?

DataInputStream and DataOutputStream allow reading and writing of primitive Java data types (int, float, double, etc.) in a machine-independent way. They provide methods like readInt(), readDouble(), writeInt(), writeDouble(), etc., which handle the conversion between primitive types and their binary representations.

15. What is the difference between System.out, System.in, and System.err?

System.out is a PrintStream used for standard output (typically the console). System.in is an InputStream used for standard input (typically keyboard input). System.err is a PrintStream used for error output, which is typically also the console but can be redirected separately from standard output.

Previous Java Interview Questions Next