Solved

How to append data to a properties file

Posted on 2012-03-30
6
408 Views
Last Modified: 2012-03-30
Hi, I have a question. I want to append data to a properties file if the properties file exists in the specified directory or else I want to create a new properties file and add data to it. Can anyone help with the complete code in Java as I am still learning  Java. Thank you.
0
Comment
Question by:vgsrikanth
  • 4
  • 2
6 Comments
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
I believe this should work:

import java.io.*;
import java.util.Properties;

public class AppendProperties {

    public AppendProperties(){
        try{


            Properties p = new Properties();
            File f0 = new File("current.properties");
            if(f0.exists()){
                 p.load(new FileInputStream("current.properties"));
            }
            
             
            
                p.setProperty("abc","123");

                p.store(new FileOutputStream("current.properties"),"");


            




        } catch(Exception ex){
            ex.printStackTrace();
        }



    }

    public static void main(String[] args) {
        new AppendProperties();
        
    }

}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
Yes, I tested it and it works - you can run it forst time when you don't have file current.properties in your default folder - it will create it with one property abc=123

then change this line

 p.setProperty("abc","123");

to line

 p.setProperty("cde","456");

and you'll see that it added new property to the current.properties file created in the fuirst run
0
 

Author Comment

by:vgsrikanth
Comment Utility
Yan, a small doubt once the file exists, I want to add something like name3=John at the end of the existing data to the properties file. The existing data is below

name1=Peter
name2=David

If the file does not exists , I want to create a new file and add name3=john to it.

Yan, can you give me the complete java code for above logic. Thank you very much.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
Comment Utility
This is the complete class which does that.
If you have a file in defaule directory called "current.properties" it will
add one more propertye name3=john to those present in the file
if there is no such file it will create new file with that property:

import java.io.*;
import java.util.Properties;

public class AppendProperties {

    public AppendProperties(){
        try{


            Properties p = new Properties();
            File f0 = new File("current.properties");
            if(f0.exists()){
                 p.load(new FileInputStream("current.properties"));
            }
            
             
            
                p.setProperty("name3","john");

                p.store(new FileOutputStream("current.properties"),"");


            




        } catch(Exception ex){
            ex.printStackTrace();
        }



    }

    public static void main(String[] args) {
        new AppendProperties();
        
    }

}
                                            

Open in new window

0
 

Author Comment

by:vgsrikanth
Comment Utility
Thank you very much Yan , it worked. You are simply great.
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
You are always welcome.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

771 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

10 Experts available now in Live!

Get 1:1 Help Now