filmov
tv
File input/output Basic tutorial in Java - Get an input from a file using java.io.FileReader
Показать описание
In this video, I will show you how to input data in a Java program using a file stored on our computer.
I will do another video in which, I will discuss how to save the output of a Java program on a file
On this channel, I have already talked about how you can get input from the keyboard and also how you can send output to the screen or dialog box. But both methods of inputting data in a program using a keyboard and even outputting results on the screen have limitations.
Let’s say for example that you have a large amount of data to input in your program, if you have to type it at the keyboard you will probably waste a lot of time, you will sometimes make mistakes while typing and this can lead to unexpected results.
So, the solution, to these problems I just mentioned, is to use files as the source of input
Now, let’s talk about the syntax
As you can see on the screen, I have used the Scanner object and have initialized it to an input source other than the standard input device
input_file is the name I have chosen to give to the input object I am creating
new is a keyword
FileReader is the special class in Java that allows us to get input from a file
And, here you have to make sure to type in the path to where your file is saved on your computer
Also, we have two backward slashes because the backward slash is an escape character that is why we need to have two backward slashes here for the program to read it as one backward slash
If I remove one backward slash, I will get an error; so I need to keep the two slashes
Since, in this case, we rather want to use a file as the source of input, let’s then use the FileReader class
Here is my file and I have written this information that is corresponding to the first name, name, age, and height of a person
So, in order to verify that my file is being used as the source of input I will store the result of these methods in appropriate variables
#codingriver
#java
#programming