Reading Text Files

Opening and Reading files in Java

Reading Text Files

Why do we need to read files?

In all programming languages, it is important to remain efficient when reading through its own code. The longer the code, the more time that the program needs to read through it.

Files can act as databases, storing data in an (un)structured way (depending on your preference!).

Storing values within the program

We will store multiple values in a String:

//  str is stored in a simple format
String str = "banana|orange|grape|apple|pear";

In this case, efficiency doesn’t really matter as we only have five different values stored in our array. But imagine if we have more, more than one hundred different values. This would slow down the program significantly as it would need to check through them all.

The other thing we have to remember is we need to separate the words as it is stored as one long string. For this we would need to remove the '|' and this could potentially make the code longer as we would need to store the values again as a new string.

Using a file outside of the program means it can be called when needed, slowing the program down less and keeping the code cleaner.

Importing the text file First we need to define a path for the program to find file:

// File path
private static final String filePath = "files\\file.txt";

In this code snippet, defining the variable as a final string prevents any modification of the path while the program is running.

Also the file is found within the project folder in Java. I have created a new folder called files within the project folder.

If you want to store the values that are in the text file, you can create a list and then use an array to store it in the list:

// List is used to store values
private static List<String> exampleText;

This next part is imperative; we must use a Catch/Try method:

// Catch try used in case the file is missing
static {
  try {
    exampleText = Arrays.asList(
    Files.readAllLines(Paths.get(filePath), Charset.defaultCharset()).get(0));
  } catch (IOException ioe) {
    System.out.println("Failed to load the file");
    System.exit(0);
  }
}

We use Catch/Try so that if the program is unable to find the text file, there is fail safe. In this instance, the IOException variable ioe will print to the console and exit the program.

The snippet below is a collection of functions that allow for the program to read the text file.

Files.readAllLines(Paths.get(filePath), Charset.defaultCharset()).get(0));

Files.readAllLines(); - Opens a file for reading the parameters within the brackets Paths.get(filePath) - Converts the path string to a Path readable by the program Charset.defaultCharset() – This gets the default encoding of the text files get(0) – An ArrayList function it returns the element at the specified position ‘0’ in the list

NOTES

The Files.readAllLines is for both Java 7 & 8.

In all these examples, you will need the following Java imports for several functions:

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.*;

Comments

blog comments powered by Disqus