Wednesday, March 1, 2023

Top 43 Java IO and File System Interview Questions with Answers for 1 to 3 Years Experienced

Good knowledge of Java IO API is important for any Java developer but many of them barely pay attention to Java IO and NIO API, and that's why they often struggle during Java interviews when a question related to Input-Output operation is asked. In the past, I have shared many Java interview questions on topics like Collections, Concurrency, JVM internals, Garbage Collection, OOP, Design Patterns, and Data structure based coding problems, which received good feedback and helped many Java developer on their Java Interview preparation. Consequently, many of my readers were also asking about Java IO and File-based interview questions and this was one of the pending items on my TODO list for quite some time.

One reason for the delay was because I was aiming to create a really big post with 100+ questions and answers in the same place, just like I did for Core Java Interview Questions earlier, but that didn't happen and this post got stuck.

This year, I am trying to clear all those backlogs and so I thought to leverage the many in-depth articles I have shared on Java IO and NIO on my blog Javarevisited and Java67, and this way I can at least publish this post out and improve upon based upon the feedback. It won't be stuck as Draft anymore and can potentially benefit Java developers looking for Java IO Interview questions.

If you are wondering what is Java IO or Java NIO then let me give you a brief overview.  Java IO is nothing but a collection of classes that will help you to deal with reading input and writing output like File, Directory, InputStream, OutputStream, Reader, Writer, BufferedReader, BufferedWriter, Scanner, etc.

They are mostly from the java.io package which is a core package in JDK but it's not automatically imported as java.lang. This package is from the start of JDK and another package java.nio was introduced for better and faster IO facilities. This is called New Input-Output or simply N-I-O package. It contains classes like Channel, ByteBuffer, FileChannel, Selector which supports Multiplexing and Asynchronous IO.

Good knowledge of all these classes are imperative for any Java developer and if you feel that you haven't touched them yet or don't have enough experience with them, I suggest you join a comprehensive Java course like The Complete Java Masterclass Course by Tim Buchalaka on Udemy to learn them in depth. They will not only help you to write better Java applications but also immensely help you during Java interviews and give you an edge over other candidates.




43 Java IO and File Interview Questions with Answers

In this article, I'll share some of the popular Java IO based Interview questions.  Some of them are easy, some of them are tricky, some are difficult, and some really tough questions. If you have worked as a Java developer for 2 to 3 years then most likely you can answer 60% of these questions, but if you can't then that's also fine because many Java developers don't get to work on IO as mostly other senior developers do.

Sometimes, these are also masked by in-house, proprietary APIs which further simplify IO work but nonetheless if you know these concepts, you can always find your way on  IO related production issues and bugs.

Anyway, without wasting any more of your time, here is my list of some of the frequently asked Java IO interview questions:


1. Which class is used to create a directory in Java? (answer)
This is tricky because there is no Directory class, File class is used to create both file and directory in Java. You can see the linked answer for more details.

2. How do you create a file and directory in Java? (answer)

3. What is the difference between InputStream and OutputStream in Java? (answer)
InputStream is used to read data from sources like File, Socket, or Console, while OutputStream is used to write data into a destination like a File, Socket, or Console.

4. Can you create a symbolic link from Java Program on supported platforms like UNIX? (answer)

5. What is the difference between FileInputStream and FileReader in Java IO? (answer)

6. What is the difference between BufferedReader and FileReader in Java? (answer)
BufferedReader is a Decorator that provides buffering for faster IO, while FileReader is used to read data from File.

7. What is the difference between BufferedReader and Scanner in Java? (answer)

8. What is the difference between Scanner and FileReader in Java? (answer)

9. What is the use of the PrintStream class in Java IO? (answer)
PrintStream is used to write data on Console, for example, System.out.println(), here out is an object of PrintStream class and we are calling println() method from that class.

40+ Java IO and Files Interview Questions with Answers


10. What is the right way to close the streams in Java? (answer)
Double-check if you have forgotten to handle the exception while closing the stream. See the linked answer for a detailed explanation.

11. Why you need to close the streams in finally block? (answer)
This is related to the previous question and we do this because finally block is always executed irrespective of exception in a try block or not.

12. Why do you need a separate try-catch block inside finally to close the input and output streams? (answer)

13. What is the file descriptor? (answer)

14. Can we monitor a directory for adding new files in Java? (answer)

15. How do you process a large file in Java? (answer)

16. What is the try-with-resource statement in Java? (answer)

17. How do you convert a file into String in Java? (answer)

18. How do you copy a file in Java? (answer)

19. How do you check the permission of a file or directory in Java? (answer)

20. How do you change the permission of a file in Java? (answer)

21. Can you open a ZIP file in Java? How? (answer)

22. What is the difference between ZipFile and ZipInputStream? (answer)

23. When does java.io.FileNotFoundException: (Access is denied) comes? How do you fix that? (answer)

24. How do you append text into an existing file in Java? (answer)

25. How do you get the default character encoding in Java? (answer)

26. Can you delete a directory with files in Java? How? (answer)

27. What is a memory-mapped file in Java? (answer)


Java IO Interview questions with answers


28. How do you read a file line by line in Java? (answer)

29. How do you check if a file is hidden in Java? (answer)

30. What is difference between getPath(), getCanonicalPath() and getAbsolutePath() in Java? (answer)

31. How do you convert an InputStream into String in Java? (answer)

32. Can we load a resource from ClassPath in Java? (answer)

33. How do you read an XML file in Java as String? (answer)

34. Can we read the Microsoft Excel (XLS and XLSX) file in Java? How? (answer)

35. How do you load a CSV file in Java? (answer)

36. How do you read the Properties file in Java? (answer)

37. How do you serialize an Object in Java? (answer)
hint:  by using ObjectInputStream and ObjectOutputStream classes

38. What is the difference between FileInputStream and ObjectInputStream in Java? (answer)

39. How do you read/write a text file in Java?  (answer)

40. How do you upload a file into a Web Server in Java? (answer)

41. How do you read a file line by line in Java 8? (answer)

42. What is RandomAccessFile? What is the benefit of using RandomAccessFile? (answer)

43. How do you convert an InputStream to Byte Array in Java? (answer)
There are InputStream constructor which accept byte array this way you can convert  byte array to InputStream in Java. Similarly, you can read data from InputStream into a byte array. 

That's all about some of the frequently asked Java IO and File/Directory based interview questions.  These are not just useful from the interview point of view but also from your daily work. Once you know how to do these kinds of stuff, you don't need to google for your day-to-day tasks, and you can complete them in less time.


Other Java Interview Questions Topics you may like to explore:
  • 101 Coding Problems for Interview and some Tips (questions)
  • 50+ Java Interview Questions for Beginners (questions)
  • Top 10 Spring Framework Interview Questions with Answers (see here)
  • 10 XML Interview Questions for Java Programmers (read here)
  • 20 Java Design Pattern Questions asked on Interviews (see here)
  • 10 popular Struts Interview Questions for Java developers (list)
  • 10 frequently asked Servlet Interview Questions with Answers (see here)
  • 10 Hibernate Interview Questions for Java EE developers (see here)
  • 10 JDBC Interview Questions for Java Programmers (questions)
  • 20 jQuery Interview Questions for Java Web Developers (list)
  • 10 Oracle Interview Questions for Java developers (see here)
  • Top 10 JSP Questions  from J2EE Interviews (read here)
  • 12 RESTful Web Services Questions from Interviews (read here)
  • Top 10 JMS and MQ Series Interview Questions and Answers (list)
  • 15 Java NIO and Networking Interview Questions with Answers (see here)
  • Top 10 XSLT Interview Questions with Answers (read more)
  • 15 Data Structure and Algorithm Questions from Java Interviews (read here)
  • Top 10 Trick Java Interview Questions and Answers (see here)
  • Top 40 Core Java Phone Interview Questions with Answers (list)
Thanks for reading this article so far. If you like these Java File and IO Interview questions on file and directory API, then please share them with your friends and colleagues. If you have any questions or feedback, then please drop a note.

P. S. - If you are just starting with Java and looking for some of the free courses to learn Java from scratch then I suggest you check out this Java Tutorial for Complete Beginners (FREE) course on Udemy to kick-start your journey. It's completely free and more than 1.2 million people have joined this course to learn Java. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.