Skip Top Navigation Bar

Read Data from a File into an ArrayList

File Class

We can use the File class to represent a file.

Constructor

We can create a File object using a String that contains the file path.

If the file pile has backslash characters (\), you will need to use two, since this is an escape character.

Scanner Class

You can use the Scanner class to read from a file.

Constructor

The Scanner constructor can be used to create a Scanner object with a File object as the parameter.

When we create a Scanner object with a File, it is possible for an exception to be thrown if the file path doesn't exist. A try..catch statement can be used.

Scanner Methods

Resources