How to

#Java #IO #NIO

Input/Output Streams in Java

Back to Table of Contents

What is the difference between IO and NIO?

Back to Table of Contents

What NIO features do you know?

Back to Table of Contents

What are “channels”?

Channels are logical (not physical) portals, abstractions of lower-level file system objects (e.g., memory-mapped files and file locks) through which data input/output occurs, while buffers serve as sources or destinations for this transferred data. When organizing output, the data to be sent is placed into a buffer, which is then passed to a channel. When input occurs, data from the channel is placed into a provided buffer.

Channels resemble pipelines that efficiently transport data between byte buffers and the entities on the other side of the channels. Channels are gateways that allow access to operating system input/output services with minimal overhead, while buffers are internal endpoints of these gateways used for transferring and receiving data.

Back to Table of Contents

What types of input/output streams exist?

Name the main classes of input/output streams.

There are two types of input/output streams:

Back to Table of Contents

In which packages are the classes for input/output streams located?

java.io, java.nio. For working with streams of compressed data, classes from the java.util.zip package are used.

Back to Table of Contents

What subclasses of the InputStream class do you know, and what are they for?

Back to Table of Contents

What is PushbackInputStream used for?

A variation of buffering that allows reading a byte and then returning it to the stream. The PushbackInputStream class provides a mechanism to “peek” into the input stream and see what will come next without extracting information.

The class has an additional method unread().

Back to Table of Contents

What is SequenceInputStream used for?

The SequenceInputStream class allows merging multiple instances of the InputStream class together. The constructor takes either a pair of InputStream objects or an Enumeration interface as arguments.

During operation, the class makes read requests from the first InputStream object until it reaches the end, and then switches to the second one. If an interface is used, the operation continues across all InputStream objects. Upon reaching the end, the associated stream is closed. Closing the stream created by the SequenceInputStream object results in the closure of all open streams.

Back to Table of Contents

Which class allows reading data from an input byte stream in the format of primitive data types?

The DataInputStream class represents an input stream and is intended for reading primitive type data such as int, double, etc. Each primitive type has its own method for reading:

Back to Table of Contents

What subclasses of the OutputStream class do you know, and what are they for?

Back to Table of Contents

What subclasses of the Reader class do you know, and what are they for?

Back to Table of Contents

What subclasses of the Writer class do you know, and what are they for?

Back to Table of Contents

What is the difference between the PrintWriter and PrintStream classes?

Primarily, the PrintWriter class employs a more advanced way of handling Unicode characters and a different output buffering mechanism: in PrintStream, the output buffer is flushed every time the print() or println() method is invoked, while in PrintWriter, you can optionally forgo automatic buffer flushing and do it explicitly using the flush() method.

Additionally, the methods in the PrintWriter class never throw exceptions. To check for errors, the checkError() method must be called explicitly.

Back to Table of Contents

What are the differences and similarities between InputStream, OutputStream, Reader, and Writer?

Back to Table of Contents

Which classes allow converting byte streams to character streams and vice versa?

Back to Table of Contents

Which classes accelerate reading/writing by using a buffer?

Back to Table of Contents

Which class is designed to work with file system elements?

File works directly with files and directories. This class allows creating new elements and obtaining information about existing ones: size, access rights, creation date and time, and the path to the parent directory.

Back to Table of Contents

What methods of the File class do you know?

The most commonly used methods of the File class are:

Back to Table of Contents

What do you know about the FileFilter interface?

The FileFilter interface is used to check whether a File object meets certain criteria. This interface contains a single method boolean accept(File pathName). This method needs to be overridden and implemented. For example:

public boolean accept(final File file) {
    return file.exists() && file.isDirectory();
}

Back to Table of Contents

How to select all items in a specific directory by criteria (for example, with a specific extension)?

The File.listFiles() method returns an array of File objects contained in a directory. The method can accept a parameter of a class that implements FileFilter. This allows including only those items in the list for which the accept method returns true (criteria can include the length of the file name or its extension).

Back to Table of Contents

What do you know about RandomAccessFile?

The java.io.RandomAccessFile class enables reading and writing data at random locations within a file. It is not part of the InputStream or OutputStream hierarchy. This is a completely separate class with its own (mostly native) methods. The reason for this is that RandomAccessFile behaves very differently from other input/output classes because it allows moving both forward and backward within a file.

RandomAccessFile has specific methods such as:

It’s worth noting that constructors for RandomAccessFile require a second argument indicating the desired access mode for the file - read-only ("r"), read and write ("rw"), or some other variations.

Back to Table of Contents

What access modes does RandomAccessFile have?

Back to Table of Contents

Which classes support reading and writing streams in a compressed format?

Back to Table of Contents

Is it possible to redirect standard input/output streams?

The System class allows you to redirect standard input, output, and error output streams using a simple call to a static method:

Back to Table of Contents

What character is the separator when specifying a path in the file system?

For various operating systems, the separator character differs. On Windows, it is \, and for Linux, it is /.

In Java, you can retrieve the separator for the current operating system by accessing the static field File.separator.

Back to Table of Contents

What is an “absolute path” and a “relative path”?

Absolute path refers to a path that points to the same location in the file system, regardless of the current working directory or other circumstances. An absolute path always starts from the root directory.

Relative path represents a path relative to the current working directory of the user or the active application.

Back to Table of Contents

A symbolic link (also “symlink”) is a special file in the file system that, instead of containing user data, holds a path to a file that should be opened when attempting to access this link (file). The target of the link can be any object, such as another link, a file, a directory, or even a non-existent file (in the latter case, attempting to open it should yield a file not found message).

Symbolic links are used for more user-friendly organization of file structures on a computer, as they:

Back to Table of Contents

Sources

Back to Interview Questions