Friday, 27 May 2016

Interview questions on Java File IO

File IO in java

  1. What is a IO stream?
    • It is a stream of data that flows from source to destination. Good example is file copying. Two streams are involved – input stream and output stream. An input stream reads from the file and stores the data in the process (generally in a temporary variable). The output stream reads from the process and writes to the destination file.
  2. What are the super most classes of all streams?
    • All the byte stream classes can be divided into two categories (input stream classes and output stream classes) and all character streams classes into two (reader classes and writer classes). There are four abstract classes from which all these streams are derived. The super most class of all byte stream classes is and for all output stream classes, Similarly for all reader classes is and for all writer classes is
  3. What is the necessity of two types of streams – byte streams and character streams?
    • Byte streams were introduced with JDK 1.0 and operate on the files containing ASCII characters. We know Java supports other language characters also known as Unicode characters. To read the files containing Unicode characters, the designers introduced character streams with JDK 1.1. As ASCII is a subset of Unicode, for the files of English characters, we can go with either byte streams or character streams.
  4. What are FileInputStream and FileOutputStream?
    • These two are general purpose classes used by the programmer very often to copy file to file. These classes work well with files containing less data of a few thousand bytes as by performance these are very poor. For larger data, it is preferred to useBufferedInputStream (or BufferedReader) and BufferedOutputStream (or BufferedWriter).
  5. Which you feel better to use – byte streams or character streams?
    • I feel personally to go with character streams as they are the latest. Many features exist in character streams that do not in byte streams like a) using BufferedReader in place of BufferedInputStreams and DataInputStream (one stream for two) and b) using newLine()method to go for next line and for this effect we must go for extra coding in byte streams etc.
  6. What is PrintStream and PrintWriter?
    • Functionally both are same but belong to two different categories – byte streams and character streams. println() method exists in both classes.
  7. Which streams are advised to use to have maximum performance in file copying? 
    • BufferedInputStream and BufferedOutputStream on byte streams side and BufferedReader and BufferedWriter on character streams side.
  8. What are piped streams?
    • There are four piped streams – PipedInputStream, PipedOutputStream, PipedReader and PipedWriter. These streams are very useful to pass data between two running threads (say, processes).
  9. What is File class?
    • It is a non-stream (not used for file operations) class used to know the properties of a file like when it was created (or modified), has read and write permissions, size etc.
  10. What is RandomAccessFile?
    • It is a special class from package which is neither a input stream nor a output stream (because it can do both). It is directly a subclass of Object class. Generally, a stream does only one purpose of either reading or writing; but RandomAccessFile can do both reading from a file and writing to a file. All the methods of DataInputStream and DataOutStream exist in RandomAccessFile.
Example : How to use FileWriter

import*;  class Simple{

public static void main(String args[]){  


FileWriter fw=new FileWriter("abc.txt");
fw.write("my name is sachin"); 
}catch(Exception e){System.out.println(e);}    

Buy New Phone :)

No comments:

Post a Comment