- 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.
- 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.
- What are the main base classes in java.io package for byte streams?
- The main base classes are
InputStream
andOutputStream
.InputStream
is used for reading byte-based data, whileOutputStream
is used for writing byte-based data.
- The main base classes are
- What is the difference between
FileReader
andFileInputStream
?FileReader
reads characters from a file, whileFileInputStream
reads raw byte streams from a file. UseFileReader
for text files for easier handling of characters, andFileInputStream
for binary files or for more control over the data.
- 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);
.
- You can append text to a file by creating a
- 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, likeFileReader
, enhancing performance.
- 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 withFileWriter
to improve performance.
- 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 andObjectInputStream
to deserialize.
- 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
- 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.
- How do you read a file line by line in Java?
- You can use a
BufferedReader
in combination with aFileReader
. Using thereadLine()
method ofBufferedReader
, you can read a file line by line.
- You can use a
- 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.
- The
- Explain the difference between
FileInputStream
andFileOutputStream
.FileInputStream
is used for reading binary data from a file, whileFileOutputStream
is used for writing binary data to a file.
- How do you check if a file exists in Java?
- Use the
exists()
method of theFile
class, e.g.,new File("filename.txt").exists();
.
- Use the
- What is a
RandomAccessFile
in Java?RandomAccessFile
allows reading from and writing to a file at any position. It can be used as both anInputStream
and anOutputStream
.
- How do you write an object to a file in Java?
- Use
ObjectOutputStream
along withFileOutputStream
to serialize and write an object to a file.
- Use
- 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.
- The
- How can you read and write UTF-8 encoded text files in Java?
- Use
InputStreamReader
andOutputStreamWriter
withFileInputStream
andFileOutputStream
, respectively, specifying “UTF-8” as the character encoding.
- Use
- What is the difference between a
BufferedReader
and aScanner
class in Java?BufferedReader
is used for reading character input stream efficiently, whileScanner
is used for parsing primitive types and strings using regular expressions.Scanner
can be slower and provides parsing capabilities.
- 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.
- What are
Path
andFiles
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.
- How do you create a temporary file in Java?
- Use the
Files.createTempFile
method from the Java NIO package to create a temporary file.
- Use the
- What is the use of the
mark()
andreset()
methods in input streams?- The
mark()
method marks the current position in the input stream. Thereset()
method repositions the stream to the position at the time themark()
method was last called.
- The
- 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.
- You can use the
- What is the difference between
read()
andreadLine()
methods in Java I/O?- The
read()
method reads a single character or a buffer of characters from the input stream, whilereadLine()
reads a line of text from aBufferedReader
.
- The
- How do you convert a byte stream to a character stream in Java?
- Use
InputStreamReader
to convert byte stream to character stream, andOutputStreamWriter
for converting character stream to byte stream.
- Use
- 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 handleUnsupportedEncodingException
.
- How do you use the
walk
method in theFiles
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.
- Use
- 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.
- How do you use
Files
andPaths
classes to check if a file is readable in Java?- Use
Files.isReadable(Paths.get("filename.txt"))
to check if the file is readable.
- Use
- Explain the difference between
Serializable
andExternalizable
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 thewriteExternal
andreadExternal
methods.
- Both are interfaces used for object serialization.