That’s why all the example are written inline inside the main method, without extra methods or classes. That would add another library for the reader to understand that has nothing to do with reading files in Java. That would take away from the examples.įor 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. 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. 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. If I was writing this code for my own project, I would use proper object-oriented principles like encapsulation, abstraction, polymorphism, etc. There is a difference between writing code for your personal or work project and writing code to explain and teach concepts. Download CodeĪll code files are available from Github. When encoding isn’t correct, you’ll see funny characters appear when reading files.Īll examples for reading text files use two encoding variations:ĭefault system encoding where no encoding is specified and explicitly setting the encoding to UTF-8. Usually, the assumption is correct but sometimes you want to be explicit when instructing your programs to read from files. EncodingĬharacter encoding is not explicitly saved with text files so Java makes assumptions about the encoding when reading files. File LocationĪll examples will read test files from C:\temp. All examples used in this article use the try-with-resources statement for importing, loading, parsing and closing files. 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. JDK7 introduced the try-with-resources statement, which simplifies the process of closing streams. Prior to JDK7, when opening a file in Java, all file resources would need to be manually closed using a try-catch-finally block. .FileUtils.readLines() – Apache Commons.This article aims to be a comprehensive collection of all the different methods. There are multiple ways of reading from files in Java. The main incompatibility is the use of lambda expressions which was introduced in Java 8. Otherwise, the code works unaltered for different Java versions. When there is an incompatibility, it will be stated in that section. All code for this article has been tested on: Java code samples don’t live in isolation, especially when it comes to Java I/O, as the API keeps evolving. 1b) Files.readAllLines() – Explicit Encoding.1a) Files.readAllLines() – Default Encoding.1b) FileReader – Explicit Encoding (InputStreamReader).That way, you will have some hard data to know the performance metrics of each method. I also put each of these methods to a real performance test and document the results. I’m going to cover reading files in multiple ways with the core Java libraries as well as two third party libraries.īut that’s not all – what good is knowing how to do something in multiple ways if you don’t know which way is best for your situation? I’m actually going to cover 15 different ways to read a file in Java. Too often, you’ll read an article that tells you one way to read a file, only to discover later there are other ways to do that. I’m going to cover all the ways you can read files in Java. This article aims to be the definitive guide for reading files in Java 7, 8 and 9. Something that worked in an earlier Java version, might not be the preferred method anymore. Something that’s quick and dirty for a small example file might not be the best method to use when you need to read a very large file. There are multiple ways of accomplishing the same task and it’s often not clear which file reading method is best to use. Reading files in Java is the cause for a lot of confusion.
0 Comments
Leave a Reply. |