File reading is the process of retrieving/grabbing data from a external file from the computer and input it into the Java program's memory for manipulation/use for code.
There are TWO WAYS we can file read, either with the Scanner class or the BufferedReader class.
To prepare for reading a file, you first need the file itself. For ease of use, all tutorials will be using the (.txt) file format, which can be easily accessed through the Notepad app on Windows.
To create the text file, open the Notepad app and create lines of text you'd like to read. Long files will take longer to read, however, so keep that in mind.
Afterwards, grab the txt file and add it to your src folder in your Eclipse application folder to add it to Eclipse itself. Here is where your file will be ready to manipulate without issues.
The scanner class is the most simple and barebones way to navigate through a file. This is used most often with reading a file line by line using a loop.
Here is an example of a code.
You need to import an ArrayList for traversing, a scanner to allow reading and extracting text from files, and io, which imports every class from the java.io library (which file reading uses a great deal of).
Despite earlier code we had in collaboration with the main class we made always being the same, in file reading it'll be quite different. We need to add a throws IOException. This essentially tells Java to throw an error automatically if the file cannot be found, lacks permission, or encounters any other unrecoverable mishap. This is more efficient so the code (and worse, your application) doesn't break.
Open the file by creating a new Scanner class, then inputting the specific name + file type of the file wanna read. Then open a empty String ArrayList to prepare for reading.
This is where we add each separate line of the text file into separate String objects in the ArrayList. The .hasNext() means while the code has a next line that has anything String on it, and the .nextLine() means that it reads the next line. The .trim() is an optional piece of code, but it is a useful one. If there are any unwanted spaces in your code, .trim() removes them for you, preventing critical errors. Think of it as an extra security measure.
The absolute best way to print the file as the text file was formatted is to use a simple loop to look through each String object and print it separately. Below is BOTH the loop and the output (and your done!!)
Like Scanner classes, a buffered reader class can read, manipulate, and use text files. But unlike the Scanner class, it can read at a much faster and LARGER pace, as it can read much larger files to take in more information. The introduction of the code is mostly the same, so nothing new has to be explained the code is below.
The split function is pretty easy to understand. It will have a String parameter, and as it traverses until it hits it, everything it hits becomes a separate String, adds it to a String array and it continues. For example, if I had line.split(";"); and a String "dog;five;six;", it would result in a String array of {"dog", "five", "six"}.
A try/catch exception is basically a type of loop hybrid that keeps trying a block of code unless it catches something (aka an error), regardless of any loops inside the 'try'. Here in our code, if it encounters ANY type of error, like internal or memory issues, it will return an error to the user.
In this 'try', it is reading a new BufferedFile to grab lines of attributes. The split function splits groups of attributes (all the same but with different numbers). Then, with all those separate arrays, it creates an object using those attributes as it is, ALSO their PIVs.
USE THIS LINE! When we pair buffered reading with the split function, we can use this line while ((line = input.readLine()) != null) to ensure that the code continues running until there are NO lines of text left to read.