How can I parse a text file?

Posted on 2006-04-22
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"?
Question by:fylix0000
    LVL 17

    Accepted Solution

    Hi fylix0000,

    Here are the basics of reading a file:

    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)
            BufferedReader in = new BufferedReader(new FileReader(filename));
            String str;
            while ((str = in.readLine()) != null)
                if (str.indexOf("#define " + key) != -1)
                    return str.substring(str.indexOf("=")+1);
            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:
    Example Usage:

    Really nice and easy with Properties Files
    Joe P

    Author Comment

    Thank you very much Joe, this solution is exact to the point and simple to do.........two thumbs up
    LVL 17

    Expert Comment

    Thanks, I'm learning how to answer questions better and better. =)

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    This video teaches viewers about errors in exception handling.

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now