Java Interview Questions - file or java.io package

1. What is File?

File is a container which store text or binary information for a permanent period.

2. What are the different operations able to operate the File?

The following are the operations operated with file.

  1. Createanewfile.
  2. Createadirectory.
  3. Deleteafile.
  4. Deleteadirectory.
  5. Checkpermission.
  6. Readdatafromfile.
  7. Writeintofile.

3. How to read data from the file in byte format with constructor

FileInputStream class is used to read data from file in byte format.

4. How to read data from the file in String format with constructor?

By the help of readLine() method.

5. Class to write data into file in byte format and syntax

FileOutputStream class is used to write data into file in byte format by the help of write().

6. Class to write data into file in String format and syntax?

By the help of Write() method.

7. How to create a file or directory in java?

By the help of public File(String pathname) ,public File(String pathname, String filename) method.

8. How to delete a file or directory in java?

By the help of public boolean delete() method.

9. State the use of I/O?

Input/output is used to process the input and produce the output based on the input.

10. What is Stream?

A stream is a sequence of data .In java a stream is composed of bytes.

11. Which package contains the classes for input & output operation?

java.io package contains all the classes for I/O operation.

12. How many stream present in java?

There are 3 Stream present in java. They are

  1. System.outwhichknownasStandardoutputstream.
  2. System.inwhichisknownasStandardinputstream.
  3. System.err-Standarderror.

13. State the use of output stream?

Output stream used to write data to a destination ,which may be a file, an array, device or socket.

14. State the diff. b/w public void Write(int) throws IOException& public void Write(byte[]) throws IOException?

Write (int) throws IOException is used to write a byte to the current output stream. Where Write (byte[]) throws IOException is used to write an array of byte to the current output stream.

15. State the diff. b/w public voidflush() throws IOException& public void close() throws IOException?

flush() method flushes the current output stream Where Close() method is used to close the current output stream.

16. Whatare the commonly used methods of InputStream class?

  1. Public abstract int read() throws IOException.
  2. public int available () throws IOException.
  3. Public void close() throws IOException.

17. Which method use to close current i/p stream?

Public void close () throws IOException.

18. Which classes used for file handling in java?

FileInputStream and FileOutputStream classes are used for file handling in java.

19. DescribeFileInputStream class &FileOutputSteram class? Or Difference between FileInputStream class & FileOutputSteramclass?

FileInputStream class: FileInputStream is used to read data from file in a character format with the help of read method. If file not present FileInputStream terminate the program abnormally by throwing FilenotFound Exception.

FileOutputStream class: FileOutputStream is used to write data into file in a byte format with the help of write method. If file not present FileOutputStream create a new file.

20. What is ByteArrayOutputStream class?

In this stream the data written into a byte array. buffer automatically grows as data is written to it.

21. State the difference between ByteArrayOutputStream() and ByteArrayOutputStream(int size)?

ByteArrayOutputStream() creates a new byte array output stream with the initial capacity of 32 bytes, throw its size increases if necessary.

Where ByteArrayOutputStream(int size) creates a new byte array output stream ,with a buffer capacity of the specified size, in bytes.

22. State the commonly used methods of ByteArrayOutputStream class & Describe it?

Public synchronized void writeTo(outputstream out) throws IOException writes the complete contents of this byteArrayOutputStream to the specified output stream.

23. What is the use of SequenceInputSteam class?

SequenceInputStream class is used to read data from multiple streams.

24. Whatare the diff. b/w SequenceInputSteam(InputSteam s1,InputStream s2) &SequenceInputSteam(Enumeration e)?

SequenceInputSteam(InputSteam s1,InputStream s2) creates a new input stream by reading the data of two input stream in order,first s1 and then s2. Where SequenceInputSteam(Enumeration e) creates a new input stream by reading the data of an enumeration whose type is input stream.

25. What is the use of BufferedOutputStream class?

It used an internal buffer which add more efficiency than to write data directly into a stream, which makes the performance fast.

26. State the use of FileWriter class?

FileWriter class is used to write character-oriented data to the file.

27. State the condition where not to use FileInputStream&FileOutputStream class?

If you have to read and write the textual information then don’t use FileInputStream & FileOutputStream class.

28. State the use of FileReader class?

FileReader class is used to read data from the file.

29. What is the use of Flush() method?

30. State the use of CharArrayWriter class?

CharArrayWriter class used to write data to multiple files.

31. Which class implements Appendable interface?

The java.io.OutputStream.flush() method flushes this output stream and forces any buffered output bytes to be written out. In otherwords The java.io.BufferedWriter.flush() method flushes the characters from a write buffer to the character or byte stream as an intended destination.

CharArrayWriter class implements Appendable interface.

32. State the ways to read data from keyboard?

There are many ways to read the data from keyboard. They are:

  1. InputStreamReader
  2. Console
  3. Scanner
  4. DataInputStream.etc..

33. What is the use of InputStreamReader class?

It connects to input stream of keyboard.and converts the byte oriented Stream into character oriented stream.

34. State the use of BufferedReader class?

BufferedReader class used to read data line by line by readLine() method.

35. State the use of Console class?

Console class use to get input from the keyboard.

36. How to get the object of Console class?

System class provides a static method named console() that returns the unique instance of console class.

37. State the diff. b/w String readLine() and String readLine(String fmt,Object…args)?

String readLine() is used to read a single line of text from the console. Where String readLine(String fmt,Object…args) provides a formatted prompt then reads the single line of text from the console.

38. State the diff. b/w char[] readPassword() and char[] readPassword(String fmt,obj…arg)?

char[] readPassword() is used to read password that is not being displayed on the console. Where char[] readPassword(String fmt,obj…arg) provides a formatted prompt then reads the password that is not being displayed in the console.

39. State the use of java.util.Scanner class?

java.util.Scanner class use to read input from the keyboard.

40. State the diff. b/w String next() and String nextLine() method?

String next() returns the next token from the scanner. Where String nextLine() moves the scanner position to the next line and returns the value as a string.

41. State the use of PrintStream class?

PrintStream class provides methods to write data to another stream.

42. State the use of Println() method?

Println() method terminates the line only.

43. State the use of Println(String s) method?

It prints the specified String value and terminates the line.

44. State the use of Println(object obj) method?

It prints the specified object value and terminate the line.

45. State the use of Printf(object format,object…arg) method?

It writes the formatted string to the current string.

46. State the use of Print(String s) method?

It prints the specified String value.

47. State the use of Print(Object obj) method?

It prints the specified object value.

48. What is deflate compression format?

It is a format in which DeflaterOutputStream and InflaterInputStream class provide mechanism to compress and uncompress data.

49. State the use of DeflaterOutputStream class?

It is use to compress the data in the default compression format .it provide facility to the other compression filters such as GZI Output stream.

50. State the use of InflaterInputStream class?

It is used to un compress the file in the deflate compression format. It provide facility to the other un compression filter such as GZI Input Stream.

51. Define Serialization & its use?

It is a mechanism of writing the state of an object into a file. It is mainly used in Hibernate ,JPA and EJB.it also use to travel objects state on the network.

52. Define Deserialization?

Extracting the state of object from file is known as Deserialization.

53. State the classes which by default implements serializable interface?

The String class and all the Wrapper class by default implements serializable interface.

54. DefineSerializable interface & its use?

It is a Marker interface.it is just used to ‘mark’ java classes which support a certain capability.

55. State the use of ObjectOutputStream class?

It is used to write primitive data types and java objects to an OutputStream.

56. Which constructor use in ObjectOutputStream class?

Public ObjectOutputStream (OutputStream out) throws IOException use in ObjectOutputStream class and its use is it create an ObjectOutputStream that writes to the specified ObjectOutputStream.

57. Which constructor use in ObjectInputStream class?

Public ObjectInputStream (InputStream in) throws IOException

58. Which constructor createObjectInputStream that read from InputStream?

Public ObjectInputStream (InputStream in) throws IOException.

59. Which method read object from the input stream?

Public final Object readObject() throwsIOException,ClassNotFoundException.

60. State the use of transient keyword?

Transient keyword use in Serialization.

61. If you de-serialize the object then what happened?

It will return the default value for transient variable.

62. What happened if file not present?

If file not present FileInputStream terminate the program abnormally by throwing FileNotFoundException but if file not present FileOutputStream create a new file.

63. Define readLine() method & its use?

readLine() is a predefined method present in BufferedReader class which use to read data from byte in a string format.

64. How to create a serializable object?

To create a Serializable object the class should be the child class of Serializable interface.

65. What is MarkerInterface?

If interface doesnot contain any method then it is known as Marker interface.

66. What public Boolean isHidden() method represent?

public Boolean isHidden () method tests whether the file named by this abstract pathname is a hidden file.

67. What public BooleancreateNewFile() throws Exception?

It automatically create a new empty file named by this abstract pathname if and only if a file with this name does not yet exit.

68. What public Boolean delete() method show?

It deletes the file or directory denoted by this abstract pathname.

69. State the work of Boolean exits()?

It test whether the file or directory denoted by this abstract pathname exists.

70. State the work of public Boolean isFile()?

It test whether the file denoted by this abstract pathname is a normal file.

71. Static public File(String pathname)?

It creates a new file instance by converting the given path name String into a abstract pathname.

72. State StringgetName()?

It returns the name of the file or directory denoted by this abstract pathname.

73. What public Booleanmkdir() represent?

it creates the directory named by this abstract pathname.

74. What class allows you to read objects directly from a stream?

FileInputStream class.

75. What is Externalizable interface?

Externalizable is an interface that enables you to define custom rules and your own mechanism for serialization.