?
Solved

How can I parse a text file?

Posted on 2006-04-22
3
Medium Priority
?
224 Views
Last Modified: 2010-03-31
Hi all, I have a file call default.cfg , it is actually a simple text file with the following format:

#define ContactSurname=William
#define ContactGiven=Bob
#define ContactPhone=1234567890
#define ContactExt=123
#define EntityName=secret101
#define Street=12 street
#define City=San Francisco
#define Country=USA
#define Zip=123456

and so on and so on.

My question is, how could I create a java program to parse these values such that I could call

return findValue("ContactSurname") then my result would be "William"?
0
Comment
Question by:fylix0000
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
BogoJoker earned 2000 total points
ID: 16516992
Hi fylix0000,

Here are the basics of reading a file:http://www.javaalmanac.com/egs/java.io/ReadLinesFromFile.html

If you take the code that is there, you can edit it like this in your findValue function, like so:
You can write this a number of ways, I will make findValue() require the filename and the key your looking for:

public String findValue(String filename, String key)
{
    try
    {
        BufferedReader in = new BufferedReader(new FileReader(filename));
        String str;
        while ((str = in.readLine()) != null)
        {
            if (str.indexOf("#define " + key) != -1)
            {
                in.close();
                return str.substring(str.indexOf("=")+1);
             }
        }
        in.close();
        return null;
    } catch (IOException e) {}
    return null; // <--- this will also happen if there is an IOException, Like file not found
}

Now, you would use that function like:
String surname = findValue("default.cfg", "ContactSurname");  // <-- surname will be William
String willNotFind = findValue("default.cfg", "BlahBlahBlah"); // <-- willNotFind will be null

I hope that helps, any questions on my code or modifications that need to be made?

Also I would suggest you look into Properties files.  It basically lets you do exactly what you want but Java has already made all the code for you.  Check it out:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html
Example Usage:
http://www.javaalmanac.com/egs/java.util/Props.html?l=rel

Really nice and easy with Properties Files
Joe P
0
 

Author Comment

by:fylix0000
ID: 16517113
Thank you very much Joe, this solution is exact to the point and simple to do.........two thumbs up
0
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16517129
Thanks, I'm learning how to answer questions better and better. =)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question