Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

i want to append the properties file line by line could you give me a code for that

Posted on 2011-05-09
4
Medium Priority
?
267 Views
Last Modified: 2012-05-11
i want to append the properties file line by line could you give me a code for that
0
Comment
  • 3
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35719108
FileOutputStream out = new FileOutputStream("myfile.txt", true);   // open in append mode
PrintWriter printer = new PrintWriter(new OutputStreamWriter(out));
printer.println("line1=abc");
printer.println("line2=def");
out.close();
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35719117
You probably want to load properties from the file
add new property and store the them to file again
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 35719191
This is the class which shows how to append properties
public class AppendProperties{
	public static void main(String[] args)
	{

        try{
        DataInputStream in = new DataInputStream(new FileInputStream("my.properties"));
        Properties prop = new Properties();
        prop.load(in);
        in.close();
        prop.setProperty("newproperty","value");
        DataOutputStream out = new DataOutputStream(new FileOutputStream("my.properties"));
        prop.store(out,"Modified properties");
        out.close();
        }catch(Exception ex){

           System.out.println(""+ ex.toString());
            ex.printStackTrace();
        }
}
}

Open in new window


Original file:
property2=bbb
property1=aaa

Open in new window

Output file:
#Modified properties
#Mon May 09 03:42:46 PDT 2011
newproperty=value
property2=bbb
property1=aaa

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35719200
You could of course append your properties line by laine and save the
property file ater you set each property
This is of course equivalent to appending the file directly, but
takes a little bit less code.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month11 days, 6 hours left to enroll

572 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