Hi all.

I want to read and update a ini file to do some processing. Here is what I have try.

public static void main(String[] args) {
        // TODO code application logic here
        new ReadINIFile().readIt();
        //new ReadINIFile().writeIt();
    }
    
    private void readIt() {
        try {
            Properties pro = new Properties();
            pro.load(new FileInputStream("temp.ini"));
            
        // Try to display them          
            System.out.println("Key is: " + pro.getProperty("Key"));            
            System.out.println("Here is: " + pro.getProperty("Here"));
            
        }
        catch(Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
    
    private void writeIt() {
        // Try to update values
        try {
            Properties pri = new Properties();
            pri.store(new FileOutputStream("temp.ini"), "Just a comment");
            pri.setProperty("Here", "New");
            
        }
        catch(IOException ioe) {
            System.out.println(ioe.getMessage());
        }
    }

Reading part is ok, it's read all data I want. When I write all content of the ini file is deleted and following is the content.

#Just a comment
#Fri May 09 10:41:51 IST 2008

Can anyone of you tell me where I'm going wrong. Here is the structure of the ini file.

[Head]
Text=Java
Key=14
Name=Old
[More]
Here=New

Hi , The problem is obvious, you are opening up a file for overwriting. so you need to open for appending. Use the FileOutputStream constructor that takes an additional boolen parameter e.g.

pri.store(new FileOutputStream("temp.ini",true), "Just a comment");

It will work as expected. Also whenever you open up a stream do close it when workl is done. hope this helps

It's wrong pal. If I append the file, what happened if my application runs thousand times. It's obvious right?

I dont get you . It depends what you are trying to achieve. Nothing is wrong here

Set your property values before you write it to the file. Setting values on the Properites object after you wrote it isn't going to update the file for you.

And do realise that there is no such thing as a section header ("[xxxxx]") in a Java Properties file so those parts won't be persisted (in fact you can't even add them to a Properties object).

store the file after setting properties. It works.

Properties pri = new Properties();pri.setProperty("Here", "New");
pri.store(new FileOutputStream("temp.ini"), "Just a comment");

If you want add new properties, open output file stream in append mode.
If you want to update existing keys, wrap proper logic around.

Cheers,
Sridhar

Hi all.

I want to read and update a ini file to do some processing. Here is what I have try.

public static void main(String[] args) {
        // TODO code application logic here
        new ReadINIFile().readIt();
        //new ReadINIFile().writeIt();
    }
    
    private void readIt() {
        try {
            Properties pro = new Properties();
            pro.load(new FileInputStream("temp.ini"));
            
        // Try to display them          
            System.out.println("Key is: " + pro.getProperty("Key"));            
            System.out.println("Here is: " + pro.getProperty("Here"));
            
        }
        catch(Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
    
    private void writeIt() {
        // Try to update values
        try {
            Properties pri = new Properties();
            pri.store(new FileOutputStream("temp.ini"), "Just a comment");
            pri.setProperty("Here", "New");
            
        }
        catch(IOException ioe) {
            System.out.println(ioe.getMessage());
        }
    }

Reading part is ok, it's read all data I want. When I write all content of the ini file is deleted and following is the content.

#Just a comment
#Fri May 09 10:41:51 IST 2008

Can anyone of you tell me where I'm going wrong. Here is the structure of the ini file.

Hai friends, same problem comes for me also, how i do this?

Hi Boazb , Thanks for your information, am used ini4j only for this problem.
Thankyou.

try this to read windows ini files in java:
http://ini4j.sourceforge.net/
Cheers,
Boaz.

Does this process Unicode text (accents and such)?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.