Java provides the Java IO Api to read from and write to data sources through input and output classes. Java IO interview questions are frequently asked in Java interview, especially during telephonic screening and initial rounds.
You knowledge of Java IO Api will also be tested sometimes during algorithmic or coding interviews which involve reading or writing data to sources such as files.
Below Java IO interview questions, answers, tips and samples will get you grounded on the fundamentals, important methods to remember, and some of the basic operations that you will perform using Java IO such as reading or writing to files etc.
Important keywords are provided at the end of below Java IO interview questions. Review them, make them a part of Java IO vocabulary, and talk about them confidently during your interview process.
I/O streams in Java programming language represent input sources from which data is read, and output destinations to which data is written. Streams support different kinds of data including bytes, characters, primitive types and objects.
Byte Streams - java.io package has two abstract classes InputStream and OutputStream that represents input stream and output stream of byte data type.
Character Streams - java.io package has two abstract classes Reader and Writer that represents input stream and output stream of character data type.
Primitive data streams- java.io package has two interfaces classes DataInput and DataOutput that represents input stream and output stream of primitive data type.
Object streams- java.io package has two interfaces ObjectInput and ObjectOutput that represents input stream and output stream of object data type.
Byte streams handle the I/O of raw binary data. Byte streams represent low-level I/O which are usually used for primitive I/O operations.
All byte stream classes in Java programming language extend from InputStream and OutpotStream. Some of the classes provided in Java programming language that are based on byte streams are - FileInputStream and FileOutputStream which handle the byte I/O of files, ByteArrayInputStream and ByteArrayOutputStream which perform byte I/O operations on a byte array, StringBufferInputStream and StringBufferOutputStream which perform byte I/O operations on strings, ObjectInputStrean and ObjectOutputStream which perform byte I/O operations on objects.
Character streams handle the I/O operations of character data sets.
All character stream classes in Java programming language implements from Reader and Writer interfaces. Some of the classes provided in Java programming language that are based on character streams are - InputStreamReader and InputStreamWriter which are byte-to-character 'bridge' streams, CharArrayReader and CharArrayWriter which perform byte I/O operations on a char arrays, StringReader and StringWriter which perform character I/O operations on strings, FileReader and FileWriter which perform byte I/O operations on character files.
Buffered streams provide buffered functionality to unbuffered streams by wrapping them.
Buffered streams perform I/O operations on buffers, and call native OS API only when the buffer is empty. This makes buffered streams highly more efficient than unbuffered streams.
Java I/O follows decorator pattern by enabling wrapping of one stream with another, i.e an unbuffered stream is wrapped with a buffered stream to provide buffered I/O operations.
Java programming language provides BufferdInputStream and BufferedOutputStream classes that wrap byte streams, and BufferedReader and BufferedWriter classes that wrap character streams.
Data streams provide binary I/O operations on primitive data (short, int, long, float, double, char, byte and boolean) and on strings. All data streams implement either the DataInput interface or the DataOutput interface
Object streams provide I/O operations on objects. Objects have to be serializable in order for object streams to operate on them...
*** See complete answer in the Java Interview Guide.
Java programming language provides the Scanner class that enables splitting of string and primitive data into separate tokens...
*** See complete answer in the Java Interview Guide.