Link to home
Start Free TrialLog in
Avatar of fylix0000
fylix0000

asked on

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:

https://www.experts-exchange.com/questions/21823634/How-can-I-parse-a-text-file.html



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 :(
ASKER CERTIFIED SOLUTION
Avatar of BogoJoker
BogoJoker

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mayank S
You should use properties files for this:

http://www.javaalmanac.com/egs/java.util/Props.html

No need to have #define there. Just use:

ContactSurname = William

so on and so forth. # is a comment in a properties-file
Avatar of BogoJoker
BogoJoker

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
>> 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:
>https://www.experts-exchange.com/questions/21823634/How-can-I-parse-a-text-file.html

Joe P =)
:-)

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
Avatar of fylix0000

ASKER

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! =)
>> #define

Remember that # and ! define comments in properties files, so those lines will not be read.