Results 1 to 6 of 6
  1. #1
    MAGNUM is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

    Default How to read and write to a file without taking out the comments in the file

    Hi all,

    I have a properties file with a few key value pairs.
    Below is the write method I'm using to change the values.

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Enumeration;
    import java.util.Properties;

    /**
    * This class is used for Read/Write values in properties file
    * @author Manet Yim (manet.yim at gmail dot com)
    */
    public class PropertiesIO {

    private String propertiesFile;
    private Properties p;

    /**
    * Initialise this class and load properties file
    * at the same time
    * @param file to load
    */
    public PropertiesIO(String file){
    this.propertiesFile = file;
    p = new Properties();
    this.loadProperties();
    }

    /**
    * Load content of properties file into memory
    */
    public void loadProperties(){
    try {
    p.load(new FileInputStream(propertiesFile));
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    /**
    * Read all value in properties file into ArrayList object
    * @return ArrayList object containing all values in properties file
    */
    public ArrayList readAllValues(){
    ArrayList values = new ArrayList();
    Enumeration e = p.elements();
    while (e.hasMoreElements()){
    values.add(e.nextElement());
    }
    return values;
    }

    /**
    * Read value that matched the key
    * @param key use to search in properties file
    * @return value that matched key
    */
    public String readValue(String key){
    return p.getProperty(key);
    }

    /**
    * Write key/value pair into properties file
    * @param key
    * @param value
    */
    public void writeProperty(String key, String value){
    p.setProperty(key, value);
    try {
    p.store(new FileOutputStream(propertiesFile), null);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    /**
    * Example starting method, demonstrate how PropertiesIO class is used
    * @param args
    */
    public static void main(String[] args){
    PropertiesIO pio = new PropertiesIO("magnum.properties");
    pio.writeProperty("DataBaseName", "abcd");

    }

    }



    The properties file before editing looks like this

    # This file is for database-specific needs.
    # abcd
    # xyzzzz
    #
    #
    #
    # One day = 86,400,000 ms.
    # One week = 604,800,000 ms.
    # Four weeks = 2,419,200,000 ms.
    DBInUse=Oracle
    #DataBaseDriver=solid.jdbc.SolidDriver
    DataBaseDriver=oracle.jdbc.driver.OracleDriver
    #BaseURL=jdbc:solid://localhost:2020
    BaseURL=jdbc:oracle:thin:@localhost:231:
    DataBaseName=qwerty
    UserId=ADMIN
    Password=ADMIN
    #




    But after editing(executing the above program), all the comments are lost and we have only the key value pairs left out.


    #Thu Feb 05 11:24:40 IST 2009
    DBInUse=Oracle
    #DataBaseDriver=solid.jdbc.SolidDriver
    DataBaseDriver=oracle.jdbc.driver.OracleDriver
    #BaseURL=jdbc:solid://localhost:2020
    BaseURL=jdbc:oracle:thin:@localhost:231:
    DataBaseName=abcd
    UserId=ADMIN
    Password=ADMIN


    My concern is ..How do I retain these comments??
    Can anyone please give me a solution ?
    I'm stuck !!


    Regards,
    Magnum

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Well, it looks as though the comments after the first property are still there. Try putting some bogus property, that you don't really use, at the top of the file say maybe
    placeholder=do not remove

  3. #3
    MAGNUM is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

    Default

    Oops..I sent you the wrong message.
    after editing ..all the comments are lost and it looks like this and I'm left with only key value pairs.



    #Thu Feb 05 11:24:40 IST 2009
    DBInUse=Oracle
    DataBaseDriver=oracle.jdbc.driver.OracleDriver
    BaseURL=jdbcracle:thin:@localhost:231:
    DataBaseName=abcd
    UserId=ADMIN
    Password=ADMIN


    Kindly let me know what do I do for this ??
    Thanks for the help

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Then you're going to have to write the file yourself, rather than using "store".

    Read all comment from the file using a buffered reader and save that as a String.

    Then when you want to store the properties create a fileoutputstream yourself, write those comments, then call store and pass it the fileoutput stream you've already written to.

    Edit: And, actually, you don't even need to write the comments yourself, simply pass them to store as the second argument.
    Last edited by masijade; 02-05-2009 at 09:02 AM.

  5. #5
    MAGNUM is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

    Default

    masijade,

    Coudl you please help me with the piece of code as I'm just a novice and do not know how to implement.

    Thanks a ton !

    Regards,
    Magnum

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

Similar Threads

  1. Open xls file in read-write mode
    By rdhaware in forum Advanced Java
    Replies: 1
    Last Post: 02-03-2009, 08:12 AM
  2. Replies: 5
    Last Post: 01-20-2009, 09:08 AM
  3. Read and Write file
    By mrdestroy in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 12:11 PM
  4. File read/write problems
    By p900128 in forum New To Java
    Replies: 4
    Last Post: 06-27-2008, 12:15 AM
  5. Read-File Write Display substring
    By hiklior in forum New To Java
    Replies: 3
    Last Post: 04-18-2008, 11:45 AM

Posting Permissions

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