Results 1 to 9 of 9
  1. #1
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default Updating conf file through java

    Hi, I am making use of IBM word cloud . It has a jar which read a conf file located external to it. This conf has name value pairs like font, palette etc. (attached below)

    Is there a way in java in which I can update the values of these based upon what the user selects from the front-end. E.g user selects font as Arial. This should go & create a new temporary conf file with font= Arial
    Attached Files Attached Files

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    See the API specs for FileWriter and BufferedWriter.

  3. #3
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default

    Yes, I went through those API's. But I am still wondering how would I identify the exact location of the font value & replace it with a new user supplied value in the text file.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You are creating a new file right? So you read lines from the old file using FileReader/BufferedReader or Scanner and write them out to the new file. If you reach the font key you replace that line with the new value.

  5. #5
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    As you have key-value pairs in your conf file, read the values in a Map and update the Map as soon as value gets changed.

    Persist the file everytime, whenever map is updated. Persistence logic you already know :-)

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Now that you (lovelesh) mention that, could even read the file into a Properties object (java will do that for you) then you update the property in that properties object. The persistence class will then take a Properties object and write it to file.

  7. #7
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default

    Great. Thanks r035198x & lovelesh. Based upon your suggestion I wrote a simple code to update the values in a conf file. Now can you please tell me whats the best way to write back to a new conf file.
    Java Code:
    String configFile = "D:\\tabconfig.txt";
    		Properties properties = new Properties();
    		InputStream is = null;
    		try {
    		is = new FileInputStream(configFile);
    		properties.load( is );
    		System.out.println(properties);
    		System.out.println(properties.getProperty("font"));
    		properties.setProperty("font", "test");
    		System.out.println(properties);
    		} catch( IOException e ) {
    //		 ...
    		} finally {
    		if( null != is ) 
    			try { is.close(); } 
    				catch( IOException e ) { /* .... */ }
    		}

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    That's where the FileWriter/BufferedWriter things can come in.
    Just loop through all the key/value pairs in there writing them to the file.

  9. #9
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default

    Thanks r035198x once again. I have written the code. Please comment if anything wrong or can be done better.
    Java Code:
    String file_name="D:\\abc.txt";
    		FileWriter fstream = new FileWriter(file_name);
    	    BufferedWriter out = new BufferedWriter(fstream);
    		for (Map.Entry entry:properties.entrySet()) {
    			String key = entry.getKey().toString();
    		    String value = entry.getValue().toString();
    		    System.out.println(key+" "+value);
    		    out.write(key+": "+value);
    		    out.write("\n");
    		}

Similar Threads

  1. Using .conf file URL check in java/servlet
    By vali786 in forum Advanced Java
    Replies: 1
    Last Post: 11-26-2010, 12:36 PM
  2. Updating a .jar File
    By Kyx in forum New To Java
    Replies: 3
    Last Post: 08-20-2010, 03:20 AM
  3. updating server side file using java bean
    By tskarthic in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-30-2009, 02:53 PM
  4. parsing and updating html file using JSP.
    By tskarthic in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-02-2009, 09:12 PM
  5. Saving and updating a single file
    By AkisV in forum Advanced Java
    Replies: 1
    Last Post: 09-27-2008, 02:42 PM

Posting Permissions

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