CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2007
    Posts
    85

    Read and update a ini file

    Hi all.

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

    Code:
    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

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Read and update a ini file

    RTFM - The API docs state that calling this method writes the comment followed by a comment line containing the date followed by the properties data.

    Your empty file problem is that you are calling this method on an empty properties file and then adding the data on subsequent lines.

    I'm not sure that using a properties object is the best way to read and write an ini file. It is designed to handle key-value pairs and has no concept of the heading/sections used in ini files to segregate data

  3. #3
    Join Date
    Sep 2007
    Posts
    85

    Re: Read and update a ini file

    So, what is the best option I have. Example would be really helpful for me.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Read and update a ini file

    So, what is the best option I have
    Well you could use some common sense and google for information. You can't be the only person in the world that wants to read and write Window's ini files from a Java app, there's almostly certainly a freely available library for doing this.

    Ok, I've looked for you - here's one called ini4J (I've no idea how good it is though).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured