Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-04-22
10
Medium Priority
?
185 Views
Last Modified: 2010-03-31
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:

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21823634.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 :(
0
Comment
Question by:fylix0000
  • 5
  • 4
10 Comments
 
LVL 17

Accepted Solution

by:
BogoJoker earned 2000 total points
ID: 16517456
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];
    try
    {
        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;
        }
        in.close();
           
        BufferedWriter out = new BufferedWriter(new FileWriter(filename));
        for (int i = 0; oldContents[i] != null; i++)
            out.write(oldContents[i]);
        out.close();

    } 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
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16518869
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
0
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16519300
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 16519336
>> In fact I posted the exact link that you have along with the java API docs on Properties

Where? Some other Q?
0
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16519343
In his original question:
>This one is answered at the link below:
>http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21823634.html

Joe P =)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16519365
:-)

I don't see why this Q was asked ;-)
0
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16520254
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
0
 

Author Comment

by:fylix0000
ID: 16520690
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.
0
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16520866
No problem, cya round! =)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16522401
>> #define

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

810 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