Java FileHandling Methodology

 

JDK Versions

Java code samples don’t live in isolation, especially when it comes to Java I/O, as the API keeps evolving. All code for this article has been tested on:

  • Java SE 7 (jdk1.7.0_80)
  • Java SE 8 (jdk1.8.0_162)
  • Java SE 9 (jdk-9.0.4)

When there is an incompatibility, it will be stated in that section. Otherwise, the code works unaltered for different Java versions. The main incompatibility is the use of lambda expressions which was introduced in Java 8.

Java File Reading Libraries

There are multiple ways of reading from files in Java. This article aims to be a comprehensive collection of all the different methods.

Closing File Resources

Prior to JDK7, when opening a file in Java, all file resources would need to be manually closed using a try-catch-finally block. JDK7 introduced the try-with-resources statement, which simplifies the process of closing streams. You no longer need to write explicit code to close streams because the JVM will automatically close the stream for you, whether an exception occurred or not. All examples used in this article use the try-with-resources statement for importing, loading, parsing and closing files.

File Location

All examples will read test files from C:\temp.

Encoding

Character encoding is not explicitly saved with text files so Java makes assumptions about the encoding when reading files. Usually, the assumption is correct but sometimes you want to be explicit when instructing your programs to read from files. When encoding isn’t correct, you’ll see funny characters appear when reading files.

All examples for reading text files use two encoding variations:
Default system encoding where no encoding is specified and explicitly setting the encoding to UTF-8.

Code Quality and Code Encapsulation

There is a difference between writing code for your personal or work project and writing code to explain and teach concepts.

If I was writing this code for my own project, I would use proper object-oriented principles like encapsulation, abstraction, polymorphism, etc. But I wanted to make each example stand alone and easily understood, which meant that some of the code has been copied from one example to the next. I did this on purpose because I didn’t want the reader to have to figure out all the encapsulation and object structures I so cleverly created. That would take away from the examples.

For the same reason, I chose NOT to write these example with a unit testing framework like JUnit or TestNG because that’s not the purpose of this article. That would add another library for the reader to understand that has nothing to do with reading files in Java. That’s why all the example are written inline inside the main method, without extra methods or classes.

My main purpose is to make the examples as easy to understand as possible and I believe that having extra unit testing and encapsulation code will not help with this. That doesn’t mean that’s how I would encourage you to write your own personal code. It’s just the way I chose to write the examples in this article to make them easier to understand.

Exception Handling

All examples declare any checked exceptions in the throwing method declaration.

The purpose of this article is to show all the different ways to read from files in Java — it’s not meant to show how to handle exceptions, which will be very specific to your situation.

So instead of creating unhelpful try catch blocks that just print exception stack traces and clutter up the code, all example will declare any checked exception in the calling method. This will make the code cleaner and easier to understand without sacrificing any functionality.

 

Related blog:

Spring boot interview questions