In Java, the ability to access files is given by the File class under the java.io package.
When reading them, this class is often paired with the Scanner class, which enables us to scan the contents of a file.

Initializing the Scanner

Suppose that you have the file input.txt which has the following entry:

A quick brown fox jumps over the lazy dog.

To read the file, we have to import the following classes:

import java.io.File
import java.io.FileNotFoundException
import java.util.Scanner

Then, we create a File object.
Upon creating a File object, we must specify the location of our file.

public static void main (String[] args) {
	File file = new File("path/to/the/file/input.txt");
}

Note
Local Paths

Depending on your IDE, your IDE may support using local paths.
For instance, in the IntelliJ IDEA, we can simply refer to the root of your project folder.

So, if your index.txt is in the src folder of your project folder, we can simply use:

File file = new File("src/input.txt");

Note
Paths on the Windows Operating System

If you’re in the Windows operating system, you may have to write your paths like these:

"path\\to\\your\\input.txt"

Next, we can declare a Scanner class.
Inside the Scanner class, we pass the File object we made on its constructor.

public static void main (String[] args) {
	File file = new File("path/to/the/file/input.txt");
	Scanner scanFile = new Scanner(file);
}

We can also do it in a single line.
We can omit the declaration of the file object and simply proceed on initializing the Scanner object directly.

public static void main (String[] args) {
	Scanner scanFile = new Scanner(new File("path/to/your/file/input.txt"));
}

Reading Files

Once you have your Scanner configured for that particular file, the Scanner reads the contents of the entire file and treats it as a single enormous input string.

For instance, if we have any of the previous setups in Initializing the Scanner (assuming that the path to the file you want to be read is correct), and the contents of the file is:

Hello world!
This is a sample message
The quick brown fox jumps over the lazy dog.

Then, we write:

public static void main (String[] args) {
	Scanner scanFile = new Scanner(new File("path/to/your/file/input.txt"));
	
	String line = scanFile.nextLine(); 
	System.out.println(line); //Outputs "Hello world!"
}

It then outputs the first line in our file input.txt, which is Hello world!


You can also read the file by each token, not just by line.

If we do this, it divides the contents into tokens, as described in Basic Terminology, then it processes them the same way as how Scanner treats these tokens, as described in Scanning the Input Stream.

To do this, we can use the next() method to retrieve the next token in the file.

public static void main (String[] args) {
	Scanner scanFile = new Scanner(new File("path/to/your/file/input.txt"));
	
	String token1, token2, token3;
	
	token1 = scanFile.next(); 
	token2 = scanFile.next();
	token3 = scanFile.next();
	
	System.out.println(token1); //Outputs "Hello"
	System.out.println(token2); //Outputs "World!"
	System.out.println(token3); //Outputs "This"
}

We can also do this for other next() methods, such as nextInt() or `nextBoolean(){:java}.
The process is also the same— you also have to handle parse errors as well.

The methods hasNext(), hasNextLine(), or other related methods also work.
This returns a boolean statement if there are still tokens (for hasNext()), tokens in a specific data type (for hasNextInt() or hasNextFloat()), or entire lines left to scan.

This can be helpful if we want to iterate over the contents of the entire file.
One way of using them is by writing a while loop, such as the one below:

public static void main (String[] args) {
	Scanner scanFile = new Scanner(new File("path/to/your/file/input.txt"));
	
	while (scanFile.hasNextLine()) {
		System.out.println(scanFile.nextLine());
	}
}

This way, once the file has nothing else to read, it can automatically stop.

Handling File Exceptions

One notable exceptions that appear when attempting to read files is the FileNotFoundException exception.
This occurs when the file you’re trying to read does not exist or cannot be found at the given directory.

In that case, we can try using a try..catch block and catch it when the exception occurs.
To set up, you can follow this format as your guide:

public static void main (String[] args) {
	try {
		Scanner scanFile = new Scanner(new File("path/to/your/file/input.txt"));
		// code here...
	} catch (FileNotFoundException e) {
		// code to execute when file cannot be found
	}
}