Java IO Interview Question Answers

Q1: What is stream?

A stream is an abstraction that either produces or consumes information. A stream is linked to a physical device by the Java I/O system. All streams behave in the same manner, even if the actual physical devices to which they are linked differ. 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.

Q2: What are two different streams?

Java 2 defines two types of streams: byte and character.
Byte streams provide a convenient means for handling input and output of bytes. Byte streams are used, for example, when reading or writing binary data.At the top are two abstract classes: InputStream and OutputStream.
Character streams provide a convenient means for handling input and output of characters. They use Unicode and, therefore, can be internationalized.At the top are two abstract classes, Reader and Writer.

Q3: How to get list of all file objects from a folder in java?

File file = new File("C:/MyFolder/");
File[] files = file.listFiles();
for(File f: files){
System.out.println(f.getName());
}

Q4: Explain few file operations?

Basic operations on File object:
canRead(): Tests whether the application can read the file
canWrite(): Tests whether the application can modify the file
createNewFile(): Tests whether the application can modify the file
delete(): Deletes the file or directory
exists(): Tests whether the file or directory exists.
getAbsolutePath(): Returns the absolute pathname string.
isDirectory(): Tests whether the file is a directory or not.
isHidden(): Tests whether the file is a hidden file or not.
list(): Returns an array of strings naming the files and directories in the directory.

Q5: How to read file content line by line in java?

BufferedReader br = null;
String strLine = "";
try {
br = new BufferedReader( new FileReader("fileName"));
while( (strLine = br.readLine()) != null){
System.out.println(strLine);
}
}

Q6: How to read input from java console in java?

BufferedReader br = null;
Reader r = new InputStreamReader(System.in);
br = new BufferedReader(r);
String str = null;
try {
do{
System.out.println("Enter Input, No to quit.");
str = br.readLine();
System.out.println(str);
} while (!str.equalsIgnoreCase("No"));
}

Q7: How to get file URI reference?

File f = new File("C:/myfile.txt");
System.out.println(f.toURI());

Q8: How to store and read objects from a file?

ObjectInputStream and ObjectOutputStream classes can help us to store and read objects from a file.
OutputStream ops = null;
ObjectOutputStream objOps = null;
try {
ops = new FileOutputStream("MyFile.txt");
objOps = new ObjectOutputStream(ops);
objOps.writeObject(emp);
objOps.flush();
}

Q9: What is the difference between Serializable and Externalizable interface in Java?

Serializable interface exists in java.io package and forms core of java serialization mechanism. It doesn't have any method and also called Marker Interface in Java. When your class implements java.io.Serializable interface it becomes Serializable in Java and gives compiler an indication that use Java Serialization mechanism to serialize this object.
Externalizable provides us writeExternal() and readExternal() method which gives us flexibility to control java serialization mechanism instead of relying on Java's default serialization.

Q10: While serializing you want some of the members not to serialize? How do you achieve it?

If you don't want any field to be part of object's state then declare it either static or transient based on your need and it will not be included during Java serialization process.

Q11: Can we transfer a Serialized object vie network?

Yes you can transfer a Serialized object via network because java serialized object remains in form of bytes which can be transmitter via network. You can also store serialized object in Disk or database as Blob.

Q12: How to get file last modified time?

File file = new File("Myfile.txt");
System.out.println(file.lastModified());

Q13: How to convert inputstream to reader or BufferedReader?

InputStream is = null;
BufferedReader bfReader = null;
try {
is = new FileInputStream("C:/sample.txt");
bfReader = new BufferedReader(new InputStreamReader(is));
String temp = null;
while((temp = bfReader.readLine()) != null){
System.out.println(temp);
}
}

Q14: How to set file permissions in java?

File scriptFile = new File("/test.sh");
System.out.println("Current file permissions:");
System.out.println("Can Execute "+scriptFile.canExecute());
System.out.println("Can Read"+scriptFile.canRead());
System.out.println("Can Write"+scriptFile.canWrite());
scriptFile.setExecutable(true);
scriptFile.setReadable(true);
scriptFile.setWritable(true);

Q15: What is RandomAccessFile?

It is a special class from java.io 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.