Java IO Interview Questions and Answers: 1. What Are The Types of I / O Streams?
Java IO Interview Questions and Answers: 1. What Are The Types of I / O Streams?
*what is stream?
*What is InputStream?
*What is OutputStream?
3. What is common and how do the following streams differ: InputStream, OutputStream, Reader, Writer?
The base class InputStream represents classes that receive data from various sources:
– an array of bytes
– a string (String)
– a file
– a pipe (pipe): data is placed from one end and extracted from the other
– a sequence of different streams that can be combined into one stream
– other sources (eg internet connection)
The class OutputStream is an abstract class that defines stream byte output. In this category are classes that determine where
your data goes: to an array of bytes (but not directly to String; it is assumed that you can create them from an array of bytes),
to a file or channel.
Character streams have two main abstract classes, Reader and Writer , which control the flow of Unicode characters. The
Reader class is an abstract class that defines character streaming input. The Writer class is an abstract class that defines
character stream output.
8. Which superstructure class allows reading data from an input byte stream in the format of primitive data types?
To read byte data (not strings), the DataInputStream class is used . In this case, you must use the classes from the
InputStream group .
To convert a string to a byte array suitable for placement into a ByteArrayInputStream stream , the getBytes () method is
provided in the Stringclass . The resulting ByteArrayInputStream is an InputStream stream suitable for passing a
DataInputStream .
When reading characters byte from the DataInputStream formatted stream using the readByte () method, any resulting value
will be considered valid, so the return value is not applicable to identify the end of the stream. Instead, you can use the
available () method , which tells you how many characters are left.
The DataInputStream class allows you to read elementary data from a stream through the DataInput interface , which defines
methods that convert elementary values into a sequence of bytes. Such streams make it easy to save binary data to a file.
9. What class add-on allows you to speed up reading / writing by using a buffer?
To do this, use classes that allow you to buffer stream
10. What classes allow you to convert byte streams to character and back?
OutputStreamWriter – the bridge between the OutputStream class and the Writer class. Characters written to the stream are
converted to bytes.
outputStreamWriter.write(“Hello World”);
outputStreamWriter.close();
InputStreamReader – analogue for reading. Using the methods of the Reader class, the bytes from the InputStream stream
are read and then converted to characters.
The java.io.File class can represent the name of a specific file, as well as the names of a group of files located in a directory. If
a class represents a directory, then its list ()method returns an array of strings with the names of all files.
To create objects of the File class, you can use one of the following constructors:
File (File dir, String name) – specify an object of the File class (directory) and file name
File (String path) – specify the path to the file without specifying the File name
File(String dirPath, Sring name) – specifies the file path and file name
File (uri uri) – specifies the URI object that describes the file
12. Which symbol is a separator when indicating the path to the EFS?
For different systems, the delimiter character is different. You can pull it out using file.separator, as well as in the static field
File.separator. For Windows, this is ‘\’.
* On stackoverflow met a statement with a link to the documentation that it is safe to use a slash ‘/’ for all systems. In a
comment the reader confirmed this.
13. How to select all EFS of a specific catalog by criterion (for example, with a certain extension)?
The File.listFiles () method returns an array of File objects contained in the directory. A method can take as an argument an
object of a class that implements FileFilter. This allows you to include a list of only those elements for which the accept
method returns true (the criterion can be the length of the file name or its extension).