Solved

How to append data to a properties file

Posted on 2012-03-30
6
411 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

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…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:

730 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