Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java IO Interview Questions and Answers: 1. What Are The Types of I / O Streams?

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Java IO Interview Questions And Answers

*what is stream?

*what are the streams attached with console?

*What is InputStream?

*What is OutputStream?

*What are the Types of InputStream?

*What are the Types of OutputStream?

* Which class we use to write data into file?


-FileWriter class is used to write a data into file.

1. What are the types of I / O streams?


There are two types of I / O streams: byte and character.

2. What are the main ancestors of I / O streams?


Byte: java.io.InputStream, java.io.OutputStream;
Symbolic: java.io.Reader, java.io.Writer;

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.

In case of errors, all class methods throw an IOException .

4. What do you know about RandomAccessFile?


The RandomAccessFile class inherits directly from Object and is not inherited from the above basic input / output classes.
Designed to work with files, supporting random access to their contents.
Working with the RandomAccessFile class resembles the use of DataInputStream and DataOutputStream combined in one
class (they implement the same DataInput and DataOutput interfaces ). In addition, the seek () method allows you to move to
a specific position and change the value stored there.
When using RandomAccessFile you need to know the file structure. The RandomAccessFile class contains methods for
reading and writing primitives and UTF-8 strings.

5. What are the file access modes?


RandomAccessFile can open in read (r) or read / write (rw) mode. There is also a “rws” mode, when a file is opened for read-
write operations and each change of file data is immediately recorded on a physical device.

6. What packages are stream classes in?


Classes of input / output streams are in java.io; With JDK 7, a more modern way of working with threads has been added –
Java NIO. Classes are in java.nio. To work with archives, classes from the java.util package are used.

7. What do you know about add-on classes?


Add-on classes endow the existing thread with additional properties. Examples of classes: BufferedOutputStream ,
BufferedInputStream , BufferedWriter – buffers the stream and improves performance.

IO Java Interview Questions And Answers

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.

Constructor: DataInputStream (InputStream stream)


Methods: readDouble (), readBoolean (), readInt ()

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

java.io.BufferedInputStream (InputStream in) || BufferedInputStream (InputStream in, int size),

java.io.BufferedOutputStream (OutputStream out) || BufferedOutputStream (OutputStream out, int size),

java.io.BufferedReader (Reader r) || BufferedReader (Reader in, int sz),

java.io.BufferedWriter (Writer out) || BufferedWriter (Writer out, int sz)

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.

OutputStream outputStream = new FileOutputStream(“c:\\data\\output.txt”);


Writer outputStreamWriter = new OutputStreamWriter(outputStream, “UTF-8”);

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.

InputStream inputStream = new FileInputStream(“c:\\data\\input.txt”);


Reader inputStreamReader = new InputStreamReader(inputStream, “UTF-8”);
int data = inputStreamReader.read();
while(data != -1){
char theChar = (char) data;
data = inputStreamReader.read();
}
inputStreamReader.close();
11. What class is designed to work with the elements of the file system (EFS)?
Unlike most I / O classes, the File class does not work with streams, but directly with files. This class allows you to get
information about the file: access rights, time and date of creation, directory path. And also navigate through the
subdirectory hierarchies.

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).

public class FileDemo {


public static void main(String[] args) {
File f = null;
File[] paths;
try{
// create new file
f = new File(“c:/test”);
// returns pathnames for files and directory
paths = f.listFiles();
// for each pathname in pathname array
for(File path:paths)
{
// prints file and directory paths
System.out.println(path);
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
}
}

14. What do you know about the FilenameFilter interface?


The FilenameFilter interface is used to check whether a File object is subject to a condition. This interface contains the only
method boolean accept (File pathName) . This method must be overridden and implemented. For example:

public boolean accept(File file) {


if (file.isDirectory()) {
return true;
} else {
String path = file.getAbsolutePath().toLowerCase();
for (int i = 0, n = extensions.length; i < n; i++) {
String extension = extensions[i];
if ((path.endsWith(extension) && (path.charAt(path.length()
– extension.length() – 1)) == ‘.’)) {
return true;
}
}
}
return false;
}
//OR
String yourPath = “insert here your path..”;
File directory = new File(yourPath);
String[] myFiles = directory.list(new FilenameFilter() {
public boolean accept(File directory, String fileName) {
return fileName.endsWith(“.txt”);
}
}
);

You might also like