I have a text file with Tag – Value format data. I want to parse this file to form a Trie. What will be the best approach?
Sample of File: (String inside “” is a tag and ‘#’ is used to comment the line.)
#Hi, this is a sample file.
"abcd" = 12;
"abcde" = 16;
"http" = 32;
"sip" = 21;
There are many ways to do this; others have mentioned that
java.util.Propertiesgets most of the job done, and is probably the most robust solution.One other option is to use a
java.util.Scanner.Scanner(File)constructor to scan a fileuseDelimiterappropriate for this formatnextInt()can be used to extract the numbersSortedMap<String,Integer>Here’s an example that scans a
Stringfor simplicity:This prints (as seen on ideone.com):
Related questions
See also