[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to append data to a properties file

Posted on 2012-03-30
6
Medium Priority
?
417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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 47

Accepted Solution

by:
for_yan earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.
Suggested Courses

656 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