How to write a to a text file afer you parse it?

Please take a look at my original question.

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"?

This one is answered at the link below:

My next question is what if I like to change the value say return setValue("ContactSurname","Henry") , this will save the change to the file so that

#define ContactSurname=William      changes to #define ContactSurname=Henry

I have tried serveral methods on my own but it seems my program just delete all the text and save as a blank file :(
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi fylix0000,

Hehe, I will go along with the other solution, with a new solution replaceValue

public void replaceValue(String filename, String key, String newValue)
    String[] oldContents = new String[100];
        BufferedReader in = new BufferedReader(new FileReader(filename));
        String str;
        for (int i = 0; str = in.readLine() != null; i++)
            if (str.indexOf("#define " + key) != -1)
                str = str.substring(0, str.indexOf("=")+1) + newValue;
            oldContents[i] = str;
        BufferedWriter out = new BufferedWriter(new FileWriter(filename));
        for (int i = 0; oldContents[i] != null; i++)

    } catch (IOException e) {}  // <--- If this happens no change, just a bad filename

Try that, it is not tested but it looks clean.
When it writes, if there is no new lines then there is somethign I can do, for now try that =)
Joe P

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:
You should use properties files for this:

No need to have #define there. Just use:

ContactSurname = William

so on and so forth. # is a comment in a properties-file
Hehe, I agree, I told him to look into property files on the link he listed above.  In fact I posted the exact link that you have along with the java API docs on Properties.

It does everything that he wants to do and all the code is already there.
Hopefully fylix is starting to agree.  No need to reinvent the wheel =)

Joe P
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Mayank SAssociate Director - Product EngineeringCommented:
>> In fact I posted the exact link that you have along with the java API docs on Properties

Where? Some other Q?
In his original question:
>This one is answered at the link below:

Joe P =)
Mayank SAssociate Director - Product EngineeringCommented:

I don't see why this Q was asked ;-)
flyix hopefully you are going to use Properties files now.
Follow the links we provided to get some quick sample code that should be enough to get you used to using Properties files.  They are really easy to work with.

Joe P
fylix0000Author Commented:
I actually kinda twiched your code abit and got what I wanted.

The thing is my txt file may not be in this format ( #define ) though out them, it may have some token or other line added in, your code seems to let me declare any unexpected but yes , i will take your advice and see if I can translate these using properties, Thanks again Joe.
No problem, cya round! =)
Mayank SAssociate Director - Product EngineeringCommented:
>> #define

Remember that # and ! define comments in properties files, so those lines will not be read.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.