The first thing when you want to read a text file is to
actually have one. So go to your desktop or anywhere else on your computer and
create a text tile and put some content into it. You can put into it random
text and numbers if you like.
Now go to Java and let’s start reading the file!
I’m going to go step by step and towards the end of the
tutorial, I will put everything together so that you can see how everything
fits.
The first step is to get the path of your file. In Windows,
you can right-click the file and then select Properties. In my case, my file is
located at C:\Users\Mukami\Desktop.
Then, create a String variable to hold the path of your file
on your computer. Don’t forget to add the name of your file at the end of the
file’s path.
String filePath = “C:\Users\Mukami\Desktop\Document1.txt”;
Now in Windows, the file location contains backslash
characters. This is a problem in Java because we know that a backslash
character in Java followed by a letter indicates a special format character.
This is why we usually have “\n” for newline and “\t” for tab, for example. In
other operating systems like Linux, however, the path of the file will contain
forward slashes, which is not a problem.
For Windows users, however, you need to change the
backslashes to forward slashes to tell Java that you are referring to a file
path.
String filePath = “C/Users/Mukami/Desktop/Document1.txt”;
The next step is to create a File object and in its
constructor, pass the file’s path, which is contained in the “filePath” variable
as follows:
File file1 = new File (filePath);
Then, we create an object of type Scanner and then pass the
object file1 of type File to its constructor like this:
Scanner scanner1 = new Scanner (file1);
So now, we can create a loop that will read the entire file
and print out the contents to the console like this:
while (scanner1.hasNextLine()) {
String
content = scanner1.nextLine();
System.out.println
(content);
}
Finally, we need to close the Scanner object to prevent
resource leakage by writing the following line:
scanner.close ();
Don’t forget to add all the necessary import statements.
Notice that scanner1.nextLine(); reads all the content in the
file, be it numerical or textual. However, what if we want to read integer
values only in the file? We use the Scanner function nextInt(). What if we want
to read double values in the file? We use nextDouble(). Here is how we read
integers and doubles respectively in the entire text file:
while (scanner1.hasNextLine()) {
String
content = scanner1.nextInt();
System.out.println
(content);
}
while (scanner1.hasNextLine()) {
String
content = scanner1.nextDouble();
System.out.println
(content);
}
Now, we know that the program will look for the file in the
specified location. But what is the file doesn’t exist or the file name or file
path is wrong? Then it means that unless we do something, the program will
crash. To avoid this, we can use a try
catch block or we can throw an exception. We will talk about Exceptions and
the try catch block in the next tutorial. For this example, we will just throw
an exception in the main method of the class like this:
public static void main (String[] args) throws
FileNotFoundException {
So now let us put everything together:
public class Person {
public
static void main (String[] args) {
String
filePath = “C/Users/Mukami/Desktop/Document1.txt”;
File file1
= new File (filePath);
Scanner
scanner1 = new Scanner (file1);
while
(scanner1.hasNextLine()) {
String
content = scanner1.nextLine();
System.out.println
(content);
}
while
(scanner1.hasNextLine()) {
Integer
contentInt = scanner1.nextInt();
System.out.println
(content);
}
while
(scanner1.hasNextLine()) {
Double
contentDouble = scanner1.nextDouble();
System.out.println
(content);
}
scanner1.close ();
}
}
Figure1: Screenshot of the contents of a text file read from
desktop.
This is one simple way of reading from text files. In another
tutorial, I will show you how to read a file in a more comprehensive way, but a
way that gives you more control over the process of reading the file.
In case you have any questions or comments, please drop them
in the comments section below and I will be more than happy to address them.
No comments:
Post a Comment