Reading Text Files and Parsing It Using Iterators and Delimiter in Java with Eclipse IDE
What Will I Learn?
In this tutorial;
- You will learn how to handle with the problem caused by opening and finding the input file.
- You will learn how to use the Scanner class.
- You will learn how to read text files.
- You will learn how to seperate String objects according to chosen delimiter character.
- You will learn simple iteration proccess.
Requirements
-Any text or inp file is required as input.
-Java SE Development Kit (Not necessarily the last version) is required.
-Eclipse IDE or any similar Interated Development Enviroment (IDE) that is designed for Java programming language is required.
Difficulty
Program does not contains too complex Java codes.
- Intermediate
Tutorial Contents
We have the text file which contains four different url as an input text file. Below figure contains the url.txt file which is going to be used as input file.
Our goal is reading this text file as input and then seperating this url links by using slash (/) as delimiter. and priting these distinct parts of the url link. Below figure contains the image of Read_Seperate_URLs.java file.
IOException
In this program, we read a file as inputs. Because of many reason, a problem can occur while opening or finding the text file. This problem results with error in our program. In order to prevent this, the throws IOException is added to the main method heading like following;
public static void main(String[] args) throws IOException{
If something goes wrong with opening or finding the input file, the error of an attempt to create a File object will be thrown to IOException.
Variables of the Program
In this program, we need to have one string object that holds the whole line. In addition to this String object, we also need to have two Scanner object. One is for text file scan (Text_Scan)and the other is for parse the pieces of the URL links strings
String link;
Scanner Text_Scan, link_Scan;
The Scanner Class
The Scanner class allows us to read text files as inputs. With below piece of code, we can directly create a scanner to read the url.txt file.
Text_Scan = new Scanner(new File("url.txt"));
At this point, the scanner Text_Scan is ready to read and proccess input from text file.
Iterators and hasNext Method
Itertors contain methods that allow us to proccess a collection of items one at a time. In this program, we need to iterator that makes the process easier such as the hasNext method. The hasNext method returns true if there any input left. For in our case, it returns true unless the scanner finishes all lines in the text file.
while(Text_Scan.hasNext()) {
link = Text_Scan.nextLine();
This while loop will be executed until all the lines is read. This outer while loop also represents the iteration proccess. Each execution of this while loop one line is read by the program. Note that the link string contains the single URL string (single line).
useDelimiter Method
By using useDelimiter method, we can parse the link string by any character. In our case, we need to seperate string with slash (/) character. Since there is again a group of item exist in single URL, we need one more scanner. For these reasons use the below code.
link_Scan= new Scanner(link);
link_Scan.useDelimiter("/");
Other Iteration
At this point, we have a group of item for each URL. By using hasNext method, again we can print all item in the group. You can find its code in the following.
while (link_Scan.hasNext())
System.out.println(" "+link_Scan.next());
Note that this inner while loop represents one iteration for each URL string.
Results of Program
When you run the program, you can see the following results.
As expected the program read the all URLs, parsed thme and printed them like following example;
URL:utopian.io/utopian-io/@aromatheraphy
utopian.io
utopian-io
@aromatheraphy
Therefore, program reachs is goal and works perferctly.
Github
You can get this program from Github.
Curriculum
You can find my other java related tutorials in below links.
Code of the Program
import java.util.Scanner;
import java.io.*;
public class Read_Seperate_URLs {
public static void main(String[] args) throws IOException{
String link;
Scanner Text_Scan, link_Scan;
Text_Scan = new Scanner(new File("url.txt"));
while(Text_Scan.hasNext()) {
link = Text_Scan.nextLine();
System.out.println("URL:"+link);
link_Scan= new Scanner(link);
link_Scan.useDelimiter("/");
while (link_Scan.hasNext())
System.out.println(" "+link_Scan.next());
System.out.println("");
}
}
}
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @aromatheraphy I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x