Solved

How to append data to a properties file

Posted on 2012-03-30
6
409 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 65
compre toata in where clue oracle 4 55
HSSFWorkbook cannot be resolved error 10 49
object oriented programming comparison 5 52
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

939 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

7 Experts available now in Live!

Get 1:1 Help Now