1. What is Java I/O?
    • Java I/O (Input/Output) is a part of Java’s java.io package which provides a set of input and output streams used to read and write data to files, external devices, or memory.
  2. What is a stream in Java I/O?
    • A stream in Java I/O represents a continuous flow of data. Streams are used to perform input and output operations in Java. They can be categorized as input streams for reading data from a source and output streams for writing data to a destination.
  3. What are the main base classes in java.io package for byte streams?
    • The main base classes are InputStream and OutputStream. InputStream is used for reading byte-based data, while OutputStream is used for writing byte-based data.
  4. What is the difference between FileReader and FileInputStream?
    • FileReader reads characters from a file, while FileInputStream reads raw byte streams from a file. Use FileReader for text files for easier handling of characters, and FileInputStream for binary files or for more control over the data.
  5. How do you append text to a file in Java?
    • You can append text to a file by creating a FileWriter object with the append flag set to true, e.g., FileWriter fw = new FileWriter("filename.txt", true);.
  6. What is a BufferedReader? How is it used?
    • BufferedReader is a class used to read text from an input stream, buffering characters for efficient reading of characters, arrays, and lines. It wraps an existing Reader, like FileReader, enhancing performance.
  7. What does the BufferedWriter class do?
    • BufferedWriter writes text to an output stream, buffering characters to provide efficient writing of single characters, arrays, and strings. It’s often used with FileWriter to improve performance.
  8. How can you serialize and deserialize an object in Java?
    • Serialization is the process of converting an object into a byte stream to save the object to a file or send it over a network. Deserialization is the reverse process. Use ObjectOutputStream to serialize and ObjectInputStream to deserialize.
  9. What is the purpose of the PrintWriter class in Java?
    • PrintWriter is used for writing formatted representations of objects to a text-output stream. It can print data representations to a file or output stream in a human-readable format.
  10. How do you read a file line by line in Java?
    • You can use a BufferedReader in combination with a FileReader. Using the readLine() method of BufferedReader, you can read a file line by line.
  11. What is the File class used for in Java?
    • The File class is used to represent file and directory pathnames in an abstract manner. It provides methods to inspect, delete, rename files or directories, and check permissions.
  12. Explain the difference between FileInputStream and FileOutputStream.
    • FileInputStream is used for reading binary data from a file, while FileOutputStream is used for writing binary data to a file.
  13. How do you check if a file exists in Java?
    • Use the exists() method of the File class, e.g., new File("filename.txt").exists();.
  14. What is a RandomAccessFile in Java?
    • RandomAccessFile allows reading from and writing to a file at any position. It can be used as both an InputStream and an OutputStream.
  15. How do you write an object to a file in Java?
    • Use ObjectOutputStream along with FileOutputStream to serialize and write an object to a file.
  16. What does the method flush() do in Java I/O streams?
    • The flush() method flushes the output stream and forces any buffered output bytes to be written out to the stream.
  17. How can you read and write UTF-8 encoded text files in Java?
    • Use InputStreamReader and OutputStreamWriter with FileInputStream and FileOutputStream, respectively, specifying “UTF-8” as the character encoding.
  18. What is the difference between a BufferedReader and a Scanner class in Java?
    • BufferedReader is used for reading character input stream efficiently, while Scanner is used for parsing primitive types and strings using regular expressions. Scanner can be slower and provides parsing capabilities.
  19. Explain the FileChannel class in Java NIO.
    • FileChannel is part of Java’s New I/O (NIO) package. It provides a high-speed channel for I/O operations with files, allowing for reading, writing, mapping, and manipulating file data.
  20. What are Path and Files classes in Java NIO?
    • Path represents a system-dependent file path. Files is a utility class that provides static methods to operate on files, directories, or other types of files, including reading, writing, and manipulating files.
  21. How do you create a temporary file in Java?
    • Use the Files.createTempFile method from the Java NIO package to create a temporary file.
  22. What is the use of the mark() and reset() methods in input streams?
    • The mark() method marks the current position in the input stream. The reset() method repositions the stream to the position at the time the mark() method was last called.
  23. How do you copy a file in Java?
    • You can use the Files.copy method from the Java NIO package to copy a file from one location to another.
  24. What is the difference between read() and readLine() methods in Java I/O?
    • The read() method reads a single character or a buffer of characters from the input stream, while readLine() reads a line of text from a BufferedReader.
  25. How do you convert a byte stream to a character stream in Java?
    • Use InputStreamReader to convert byte stream to character stream, and OutputStreamWriter for converting character stream to byte stream.
  26. What is the StandardCharsets class in Java?
    • StandardCharsets defines constants for standard charsets (like UTF-8, US-ASCII, etc.), ensuring portability across Java platforms without needing to handle UnsupportedEncodingException.
  27. How do you use the walk method in the Files class to list all files in a directory?
    • Use Files.walk(Path start, int maxDepth) to iterate over all files in a directory and its subdirectories up to a certain depth.
  28. What is the advantage of using BufferedOutputStream?
    • BufferedOutputStream adds a buffering layer to an output stream, reducing the number of write operations to the destination and improving performance.
  29. How do you use Files and Paths classes to check if a file is readable in Java?
    • Use Files.isReadable(Paths.get("filename.txt")) to check if the file is readable.
  30. Explain the difference between Serializable and Externalizable in Java.
    • Both are interfaces used for object serialization. Serializable is a marker interface signaling that an object can be serialized automatically. Externalizable allows customizing the serialization and deserialization process by implementing the writeExternal and readExternal methods.