Top Java IO Interview Questions (2024) | TechGeekNext

Top Java IO Interview Questions (2024)

  1. What is Java IO?
  2. What is Java NIO, and how does it differ from Java IO?
  3. What is InputStream and OutputStream in Java IO?
  4. What is IO Stream in Java?
  5. What classes are available in the Java IO File Classes API?
  6. What is FileInputStream in Java?
  7. What is FileOutputStream in Java?
  8. How to read single character from file in Java FileInputStream?
  9. How to read all characters from file in Java FileInputStream?
  10. How to handle Java IO Exception from Java 7?
  11. What is Java RandomAccessFile in Java IO?
  12. What is PipedReader in Java IO?
  13. What is PushbackInputStream in Java IO?

Q: What is Java IO?
Ans:

Java IO is a built-in Java API that allows you to read and write data (input and output). For example, read data from a file or over the network and then write to a file or over the network. The Java IO API can be found in the Java IO java.io package.

Q: What is Java NIO, and how does it differ from Java IO?
Ans:

Java also includes another IO API known as Java NIO. It includes classes that perform many of the same functions as the Java IO and Java Networking APIs, but Java NIO can operate in a non-blocking mode. Non-blocking IO can provide a significant performance improvement over blocking IO in some cases.

Q: What is InputStream and OutputStream in Java IO?
Ans:

An InputStream or a Reader is required by a programme that needs to read data from a source. An OutputStream or a Writer is required by a programme that needs to write data to a certain location.

Take a look at our suggested post :

Q: What is IO Stream in Java?
Ans:

IO streams are a fundamental concept in Java IO. A stream can either be read from or written to. A data source or a data destination is connected to a stream. In Java IO, streams can be either byte-based (reading and writing bytes) or character-based (reading and writing characters) (reading and writing characters).

Q: What classes are available in the Java IO File Classes API?
Ans:

The following classes are available in the Java IO API and are important to work with files in Java.

  • File
  • RandomAccessFile
  • FileInputStream
  • FileReader
  • FileOutputStream
  • FileWriter
  • Q: What is FileInputStream in Java?
    Ans:

    The Java FileInputStream class (java.io.FileInputStream) allows us to read a file's contents as a stream of bytes. FileInputStream is a subclass of InputStream. This signifies that the Java FileInputStream is used as an InputStream.

    Q: What is FileOutputStream in Java?
    Ans:

    Package Manager is used to install, update or uninstall packages/libraries/dependencies into your application. Gulp requires Node and its package manager, npm (Node Package Manager) which is responsible for installing the gulp plugins.

    Q: How to read single character from file in Java FileInputStream?
    Ans:

    package com.techgeeknext;
    
    import java.io.FileInputStream;
    
    public class FileInputStreamExample {
      public static void main(String args[]){
      try{
        FileInputStream fileInputStream=new FileInputStream("D:\\notes.txt");
        int num=fileInputStream.read();
        System.out.print((char)num);
        fileInputStream.close();
      }catch(Exception exception){
       System.out.println(exception);
      }
     }
    }
    
    Text in notes.txt file contains "TechGeekNext".
    
    Output:
    T

    Q: How to read all characters from file in Java FileInputStream?
    Ans:

    package com.techgeeknext;
    
    import java.io.FileInputStream;
    
    public class FileInputStreamExample {
    public static void main(String args[]){
       try{
         FileInputStream fileInputStream=new FileInputStream("D:\\notes.txt");
         int num=0;
         while((num=fileInputStream.read())!=-1){
          System.out.print((char)num);
         }
         fileInputStream.close();
       }catch(Exception exception){
       System.out.println(exception);
      }
      }
    }
    
    Text in notes.txt file contains "TechGeekNext".
    
    Output:
    TechGeekNext

    Q: How to handle Java IO Exception from Java 7?
    Ans:

    From Java 7 onwards, a new exception handling mechanism called "try with resources" is available. This exception handling technique is designed to handle exceptions while accessing resources that must be correctly closed after usage, such as InputStream and OutputStream.

    private static void displayFileData() throws IOException {
    
        try(FileInputStream inputStream = new FileInputStream("notes.txt")) {
    
            int data = inputStream.read();
            while(data != -1){
                System.out.print((char) data);
                data = inputStream.read();
            }
        }
    }
    

    Q: What is Java RandomAccessFile in Java IO?
    Ans:

    RandomAccessFile class allows you to read and write to a random access file. A random access file works in the same way as a large array of bytes. EOFException is issued if the end-of-file is reached before the desired number of bytes have been read. It's an IOException type.

    private byte[] readFromFile(String inputFilePath, int position, int size)
                throws IOException {
      RandomAccessFile inputFile = new RandomAccessFile(inputFilePath, "r");
      inputFile.seek(position);
      byte[] data = new byte[size];
      inputFile.read(data);
      inputFile.close();
      return data;
    }
    private void writeToFile(String inputFilePath, String data, int position)
            throws IOException {
        RandomAccessFile inputFile = new RandomAccessFile(inputFilePath, "rw");
        inputFile.seek(position);
        inputFile.write(data.getBytes());
        inputFile.close();
    }
    

    Q: What is PipedReader in Java IO?
    Ans:

    The Java PipedReader class (java.io.PipedReader) lets you read a pipe's contents as a stream of characters. As a result, it behaves similarly to a PipedInputStream, except that the PipedInputStream is byte-based rather than character-based. To put it another way, the PipedReader is designed to read text.

    A PipedWriter must be connected to a Java PipedReader. PipedReader and PipedWriter are frequently utilised by separate threads. A PipedReader can only be connected to one PipedWriter at a time.

    PipedWriter pipedWriter = new PipedWriter();
    PipedReader pipedReader = new PipedReader(pipedWriter);
    
    int data = pipedReader.read();
    while(data != -1) {
        data = pipedReader.read();
        System.out.print(data);
    }
    pipedReader.close();
    

    Q: What is PushbackInputStream in Java IO?
    Ans:

    Pushback is a technique for reading a byte from an input stream and then returning it (i.e. "pushing back") to the stream. This concept is implemented by the PushbackInputStream class. It allows you to "peek" at what's coming from an input stream without interfering with it. It is an extension of FilterInputStream.

    public static void main(String arg[]) throws IOException
    {
       PrintWriter writer = new PrintWriter(System.out, true);
       String greetText = "Hello TechGeekNext User";
       byte data[] = greetText.getBytes();
       ByteArrayInputStream byteStream = new ByteArrayInputStream(data);
       PushbackInputStream pushStrem = new PushbackInputStream(byteStream);
    
       writer.println("total bytes: " + pushStrem.available());
    
       writer.println("validate if mark supported :" + pushStrem.markSupported());
    
       writer.close();
    }

    Recommendation for Top Popular Post :