Solved

How to append data to a properties file

Posted on 2012-03-30
6
410 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
ID: 37789751
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
ID: 37789756
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
ID: 37789765
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37789769
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
ID: 37789781
Thank you very much Yan , it worked. You are simply great.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37789811
You are always welcome.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java exception bubble up 2 21
swing controls 2 16
Fast way to search item into Java Array (Rhino compatible) 2 31
Java Eclipse Loop 3 20
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

856 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