Java IO Interview Questions And Answers

Interview Questions

17 Java IO Interview Questions And Answers – Input Output in Java Interview Questions For Experienced 2018 from Codingcompiler. Test your Java I/O knowledge by answering these tricky interview questions on Java IO. Let’s start learning Java IO interview questions and prepare for Java interviews. All the best for your future and happy learning.

Java IO Interview Questions

  1. What are the types of I / O streams?
  2. What are the main ancestors of I / O streams?
  3. What is common and how do the following streams differ: InputStream, OutputStream, Reader, Writer?
  4. What do you know about RandomAccessFile?
  5. What are the file access modes?
  6. What packages are stream classes in?
  7. What do you know about add-on classes?
  8. Which superstructure class allows reading data from an input byte stream in the format of primitive data types?
  9. What class add-on allows you to speed up reading / writing by using a buffer?
  10. What classes allow you to convert byte streams to character and back?
  11. What class is designed to work with the elements of the file system (EFS)?
  12. Which symbol is a separator when indicating the path to the EFS?
  13. How to select all EFS of a specific catalog by criterion (for example, with a certain extension)?
  14. What do you know about the FilenameFilter interface?
  15. What is serialization?
  16. What are the conditions for “successful” serialization of an object?
  17. What classes allow you to archive objects?

Related Java Interview Questions

  1. Java String Interview Questions
  2. Java Collections Interview Questions
  3. Java Exceptions Interview Questions
  4. Java OOPS Interview Questions
  5. Core Java Interview Questions
  6. JSF Interview Questions
  7. JSP Interview Questions
  8. JPA Interview Questions
  9. Spring Framework Interview Questions
  10. Spring Boot Interview Questions
  11. Core Java Multiple Choice Questions
  12. 60 Java MCQ Questions And Answers
  13. Aricent Java Interview Questions
  14. Accenture Java Interview Questions
  15. Advanced Java Interview Questions For 5 8 10 Years Experienced
  16. Core Java Interview Questions For Experienced

17 Java IO Interview Questions And Answers

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.

1

2

3

4

5

6

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.

1

2

3

4

5

6

7

8

9

10

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

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”);

   }

});

15. What is serialization?

Serialization is the process of maintaining the state of an object in a sequence of bytes; deserialization is the process of restoring an object from these bytes. The Java Serialization API provides a standard mechanism for creating serializable objects.

16. What are the conditions for “successful” serialization of an object?

To be serializable, the class must implement the Serializable interface tag. Also, all attributes and subtypes of the class being serialized must be serializable. If the ancestor class was non-realizable, then this superclass must contain an accessible (public, protected) constructor without parameters to initialize the fields.

17. What classes allow you to archive objects?

DeflaterOutputStream, InflaterInputStream, ZipInputStream, ZipOutputStream, GZIPInputStream, GZIPOutputStream.

An example program:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

package com.Codingcompiler;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class ZipOutputStreamTest {

   public static void main(String… args) throws IOException {

       String source=”D:/page/file.txt”;

       File sfile= new File(source);

       String dest=”D:/page/file.zip”;

       File dfile= new File(dest);

       FileInputStream fis= new FileInputStream(sfile);

       FileOutputStream fos= new FileOutputStream(dfile);

       ZipOutputStream zos= new ZipOutputStream(fos);

       ZipEntry ze= new ZipEntry(source);

       //begins writing a new zip file and sets the position to the start of data

       zos.putNextEntry(ze);

       byte[] buf = new byte[1024];

       int len;

       while((len=fis.read(buf))>0){

           zos.write(buf, 0, len);

       }

       System.out.println(“File created:”+dest);

       fis.close();

       zos.close();

   }

Must Read Java Interview Questions Books

RELATED INTERVIEW QUESTIONS

  1. Java String Interview Questions
  2. Java Collections Interview Questions
  3. Java Exceptions Interview Questions
  4. Java OOPS Interview Questions
  5. Core Java Interview Questions
  6. JSF Interview Questions
  7. JSP Interview Questions
  8. JPA Interview Questions
  9. Spring Framework Interview Questions
  10. Spring Boot Interview Questions
  11. Core Java Multiple Choice Questions
  12. 60 Java MCQ Questions And Answers
  13. Aricent Java Interview Questions
  14. Accenture Java Interview Questions
  15. Advanced Java Interview Questions For 5 8 10 Years Experienced
  16. Core Java Interview Questions For Experienced
  17. GIT Interview Questions And Answers
  18. Network Security Interview Questions
  19. CheckPoint Interview Questions
  20. Page Object Model Interview Questions
  21. Apache Pig Interview Questions
  22. Python Interview Questions And Answers
  23. Peoplesoft Integration Broker Interview Questions
  24. PeopleSoft Application Engine Interview Questions
  25. RSA enVision Interview Questions
  26. RSA SecurID Interview Questions
  27. Archer GRC Interview Questions
  28. RSA Archer Interview Questions
  29. Blockchain Interview Questions
  30. Commvault Interview Questions

Leave a Reply

Your email address will not be published. Required fields are marked *