Java Code to modify properties file and preserve comments

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 63543 Views - Last Post: 26 August 2010 - 11:55 AM Rate Topic: -----

#1 jms9900  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-June 08

Java Code to modify properties file and preserve comments

Post icon  Posted 03 June 2008 - 01:57 PM

The solution below (which uses the Java Properties class) ignores comments in the file and the result is they are removed from the properties file being edited once the application has completed processing and modifying the properties file. Is there some other effective/efficient way to do this. Its nice to use the Properties class since it does so much for you... only shortfall is it ignores # or ! descriptive documentation comments about the properties (they are lost when the file is changed by the following solution).



// -----------------------------------------------------------------------------
// PropertyFiles.java
// -----------------------------------------------------------------------------
 
import java.util.Properties;
import java.util.Enumeration;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;

/**
 * -----------------------------------------------------------------------------
 * Used to provide an example of how to get/modify/save Properties files
 * while preserving comments in the file.
 * -----------------------------------------------------------------------------
 */

public class PropertyFiles {


	private static void printProperties(Properties p, String s) {

		System.out.println();
		System.out.println();

		System.out.println(s);

		System.out.println();

		p.list(System.out);
		System.out.println();
	}


	private static void saveProperties(Properties p, String fileName) {

		OutputStream outPropFile;

		try {
			outPropFile = new FileOutputStream(fileName);
			p.store(outPropFile, "Properties File to the Test Application");
			outPropFile.close();
		} catch (IOException ioe) {
			System.out.println("I/O Exception.");
			ioe.printStackTrace();
			System.exit(0);
		}

	}


	private static Properties loadProperties(String fileName) {

		InputStream inPropFile;
		Properties tempProp = new Properties();

		try {
			inPropFile = new FileInputStream(fileName);
			tempProp.load(inPropFile);
			inPropFile.close();
		} catch (IOException ioe) {
			System.out.println("I/O Exception.");
			ioe.printStackTrace();
			System.exit(0);
		}

		return tempProp;

	}


	private static Properties alterProperties(Properties p) {

		Properties newProps = new Properties();
		Enumeration enumProps = p.propertyNames();
		String key = "";

		while ( enumProps.hasMoreElements() ) {

			key = (String) enumProps.nextElement();

			if (!key.equals("fake_entry")) {
				if (key.equals("log_level")) {
					newProps.setProperty(key, "3");
				} else {
					newProps.setProperty(key, p.getProperty(key));
				}
			}

		}

		return newProps;

	}


	/**
	 * Sole entry point to the class and application.
	 * @param args Array of String arguments.
	 */
	public static void main(String[] args) {

		final String PROPFILE= "Application.properties";
		Properties myProp;
		Properties myNewProp;

		// Input Properties File
		myProp = loadProperties(PROPFILE);
		printProperties(myProp, "Loaded Properties");

		// Modified Properties File
		myNewProp = alterProperties(myProp);
		printProperties(myNewProp, "After Modifying Properties");
		saveProperties(myNewProp, PROPFILE);

	}

}



This post has been edited by jms9900: 04 June 2008 - 07:24 AM


Is This A Good Question/Topic? 0

Replies To: Java Code to modify properties file and preserve comments

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Java Code to modify properties file and preserve comments

Posted 03 June 2008 - 03:16 PM

Seem it is the day of the XML questions. New fashion ?
I'll try to understand what you want to achieve.
Was This Post Helpful? 0
  • +
  • -

#3 jms9900  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-June 08

Re: Java Code to modify properties file and preserve comments

Posted 03 June 2008 - 03:33 PM

The code basically needs to be able to read/modify/save a properties file that has comments within the file without losing the comments.

My attached code allows updates to property key/value entries but does not allow the comments to be preserved since the Properties class is being used.
Was This Post Helpful? 0
  • +
  • -

#4 sandykatkat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-January 09

Re: Java Code to modify properties file and preserve comments

Posted 08 January 2009 - 05:07 PM

Have you found a solution? I am facing the same problem, too.
Was This Post Helpful? 0
  • +
  • -

#6 waltf  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 10
  • Joined: 23-December 09

Re: Java Code to modify properties file and preserve comments

Posted 23 December 2009 - 09:05 AM

Here is a solution I created for a project at work, I stripped out the package name and a few Logging TRACE printouts (note that I didn't test it with these mods).

The following code will load/save a properties file and preserve comments and blank lines. There is even a method (addLine) to add an additional blank or comment line to the end of the loaded properties, which will be in the file if you then save the properties to a file.


import java.io.*;
import java.util.*;

/**
 * The CommentedProperties class is an extension of java.util.Properties
 * to allow retention of comment lines and blank (whitespace only) lines
 * in the properties file.
 * 
 * Written for Java version 1.4
 */
public class CommentedProperties extends java.util.Properties {

	/**
	 * Use a Vector to keep a copy of lines that are a comment or 'blank'
	 */
	public Vector lineData = new Vector(0, 1);

	/**
	 * Use a Vector to keep a copy of lines containing a key, i.e. they are a property.
	 */
	public Vector keyData = new Vector(0, 1);

	/**
	 * Load properties from the specified InputStream. 
	 * Overload the load method in Properties so we can keep comment and blank lines.
	 * @param   inStream   The InputStream to read.
	 */
	public void load(InputStream inStream) throws IOException
	{
		// The spec says that the file must be encoded using ISO-8859-1.
		BufferedReader reader =
		new BufferedReader(new InputStreamReader(inStream, "ISO-8859-1"));
		String line;

		while ((line = reader.readLine()) != null) {
			char c = 0;
			int pos = 0;
			// Leading whitespaces must be deleted first.
			while ( pos < line.length()
					&& Character.isWhitespace(c = line.charAt(pos))) {
				pos++;
			}

			// If empty line or begins with a comment character, save this line
			// in lineData and save a "" in keyData.
			if (	(line.length() - pos) == 0
					|| line.charAt(pos) == '#' || line.charAt(pos) == '!') {
				lineData.add(line);
				keyData.add("");
				continue;
			}

			// The characters up to the next Whitespace, ':', or '='
			// describe the key.  But look for escape sequences.
			// Try to short-circuit when there is no escape char.
			int start = pos;
			boolean needsEscape = line.indexOf('\\', pos) != -1;
			StringBuffer key = needsEscape ? new StringBuffer() : null;

			while ( pos < line.length()
					&& ! Character.isWhitespace(c = line.charAt(pos++))
					&& c != '=' && c != ':') {
				if (needsEscape && c == '\\') {
					if (pos == line.length()) {
						// The line continues on the next line.  If there
						// is no next line, just treat it as a key with an
						// empty value.
						line = reader.readLine();
						if (line == null)
							line = "";
						pos = 0;
						while ( pos < line.length()
								&& Character.isWhitespace(c = line.charAt(pos)))
							pos++;
					} else {
						c = line.charAt(pos++);
						switch (c) {
							case 'n':
								key.append('\n');
								break;
							case 't':
								key.append('\t');
								break;
							case 'r':
								key.append('\r');
								break;
							case 'u':
								if (pos + 4 <= line.length()) {
									char uni = (char) Integer.parseInt
											   (line.substring(pos, pos + 4), 16);
									key.append(uni);
									pos += 4;
								}   // else throw exception?
								break;
							default:
								key.append(c);
								break;
						}
					}
				} else if (needsEscape)
					key.append(c);
			}

			boolean isDelim = (c == ':' || c == '=');

			String keyString;
			if (needsEscape)
				keyString = key.toString();
			else if (isDelim || Character.isWhitespace(c))
				keyString = line.substring(start, pos - 1);
			else
				keyString = line.substring(start, pos);

			while ( pos < line.length()
					&& Character.isWhitespace(c = line.charAt(pos)))
				pos++;

			if (! isDelim && (c == ':' || c == '=')) {
				pos++;
				while ( pos < line.length()
						&& Character.isWhitespace(c = line.charAt(pos)))
					pos++;
			}

			// Short-circuit if no escape chars found.
			if (!needsEscape) {
				put(keyString, line.substring(pos));
				// Save a "" in lineData and save this
				// keyString in keyData.
				lineData.add("");
				keyData.add(keyString);
				continue;
			}

			// Escape char found so iterate through the rest of the line.
			StringBuffer element = new StringBuffer(line.length() - pos);
			while (pos < line.length()) {
				c = line.charAt(pos++);
				if (c == '\\') {
					if (pos == line.length()) {
						// The line continues on the next line.
						line = reader.readLine();

						// We might have seen a backslash at the end of
						// the file.  The JDK ignores the backslash in
						// this case, so we follow for compatibility.
						if (line == null)
							break;

						pos = 0;
						while ( pos < line.length()
								&& Character.isWhitespace(c = line.charAt(pos)))
							pos++;
						element.ensureCapacity(line.length() - pos +
											   element.length());
					} else {
						c = line.charAt(pos++);
						switch (c) {
							case 'n':
								element.append('\n');
								break;
							case 't':
								element.append('\t');
								break;
							case 'r':
								element.append('\r');
								break;
							case 'u':
								if (pos + 4 <= line.length()) {
									char uni = (char) Integer.parseInt
											   (line.substring(pos, pos + 4), 16);
									element.append(uni);
									pos += 4;
								}   // else throw exception?
								break;
							default:
								element.append(c);
								break;
						}
					}
				} else
					element.append(c);
			}
			put(keyString, element.toString());
			// Save a "" in lineData and save this
			// keyString in keyData.
			lineData.add("");
			keyData.add(keyString);
		}
	}

	/**
	 * Write the properties to the specified OutputStream.
	 * 
	 * Overloads the store method in Properties so we can put back comment	
	 * and blank lines.													  
	 * 
	 * @param out	The OutputStream to write to.
	 * @param header Ignored, here for compatability w/ Properties.
	 * 
	 * @exception IOException
	 */
	public void store(OutputStream out, String header) throws IOException
	{
		// The spec says that the file must be encoded using ISO-8859-1.
		PrintWriter writer
		= new PrintWriter(new OutputStreamWriter(out, "ISO-8859-1"));

		// We ignore the header, because if we prepend a commented header
		// then read it back in it is now a comment, which will be saved
		// and then when we write again we would prepend Another header...

		String line;
		String key;
		StringBuffer s = new StringBuffer ();

		for (int i=0; i<lineData.size(); i++) {
			line = (String) lineData.get(i);
			key = (String) keyData.get(i);
			if (key.length() > 0) {  // This is a 'property' line, so rebuild it
				formatForOutput (key, s, true);
				s.append ('=');
				formatForOutput ((String) get(key), s, false);
				writer.println (s);
			} else {  // was a blank or comment line, so just restore it
				writer.println (line);
			}
		} 
		writer.flush ();
	}

	/**
	 * Need this method from Properties because original code has StringBuilder,
	 * which is an element of Java 1.5, used StringBuffer instead (because
	 * this code was written for Java 1.4)
	 * 
	 * @param str	- the string to format
	 * @param buffer - buffer to hold the string
	 * @param key	- true if str the key is formatted, false if the value is formatted
	 */
	private void formatForOutput(String str, StringBuffer buffer, boolean key)
	{
		if (key) {
			buffer.setLength(0);
			buffer.ensureCapacity(str.length());
		} else
			buffer.ensureCapacity(buffer.length() + str.length());
		boolean head = true;
		int size = str.length();
		for (int i = 0; i < size; i++) {
			char c = str.charAt(i);
			switch (c) {
				case '\n':
					buffer.append("\\n");
					break;
				case '\r':
					buffer.append("\\r");
					break;
				case '\t':
					buffer.append("\\t");
					break;
				case ' ':
					buffer.append(head ? "\\ " : " ");
					break;
				case '\\':
				case '!':
				case '#':
				case '=':
				case ':':
					buffer.append('\\').append(c);
					break;
				default:
					if (c < ' ' || c > '~') {
						String hex = Integer.toHexString(c);
						buffer.append("\\u0000".substring(0, 6 - hex.length()));
						buffer.append(hex);
					} else
						buffer.append(c);
			}
			if (c != ' ')
				head = key;
		}
	}

	/**
	 * Add a Property to the end of the CommentedProperties. 
	 * 
	 * @param   keyString	 The Property key.
	 * @param   value		 The value of this Property.
	 */
	public void add(String keyString, String value)
	{
		put(keyString, value);
		lineData.add("");
		keyData.add(keyString);
	}

	/**
	 * Add a comment or blank line or comment to the end of the CommentedProperties. 
	 * 
	 * @param   line The string to add to the end, make sure this is a comment
	 *			   or a 'whitespace' line.
	 */
	public void addLine(String line)
	{
		lineData.add(line);
		keyData.add("");
	}
}


Was This Post Helpful? 1

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,992
  • Joined: 20-September 08

Re: Java Code to modify properties file and preserve comments

Posted 23 December 2009 - 01:51 PM

Just treat the file as a Collection of String. You can find out which lines are comments pretty easily

http://technojeeves..../74-string-list
Was This Post Helpful? 0
  • +
  • -

#8 waltf  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 10
  • Joined: 23-December 09

Re: Java Code to modify properties file and preserve comments

Posted 23 December 2009 - 04:25 PM

Yes, g00se, but you still have to process the properties, including having key / value pairs cross multiple lines, keeping track of which strings in the collection are comments vs. properties, writing the changes of a property back to the proper string in the collection, etc.

The code above is adapted from the properties source, if I was to write it from scratch, yes I would probably do it differently (I wrote it about 3-4 years ago, for Java 1.4...).

:D

-waltf
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Java Code to modify properties file and preserve comments

Posted 23 December 2009 - 05:20 PM

Back to the basic :)
If you want to use the nice features of the Properties class which use a HashMap forget about your comments
If you want to save the comments: write your own
Was This Post Helpful? 0
  • +
  • -

#10 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Java Code to modify properties file and preserve comments

Posted 23 December 2009 - 06:35 PM

Also, please create your own threads, instead of necroing ones from 2008
Was This Post Helpful? 0
  • +
  • -

#11 waltf  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 10
  • Joined: 23-December 09

Re: Java Code to modify properties file and preserve comments

Posted 24 December 2009 - 10:31 AM

View Posterik.price, on 23 Dec, 2009 - 05:35 PM, said:

Also, please create your own threads, instead of necroing ones from 2008



Hi Erik,

This post was in response to an asked question - Yes, the question was a little dated, but someone might still find it useful.

waltf
Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Java Code to modify properties file and preserve comments

Posted 24 December 2009 - 10:38 AM

Well, still, it's rather old, and you could have linked to it...
Was This Post Helpful? 0
  • +
  • -

#13 garydeng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-January 10

Re: Java Code to modify properties file and preserve comments

Posted 05 January 2010 - 02:57 PM

View Postwaltf, on 23 Dec, 2009 - 08:05 AM, said:

Here is a solution I created for a project at work, I stripped out the package name and a few Logging TRACE printouts (note that I didn't test it with these mods).

The following code will load/save a properties file and preserve comments and blank lines. There is even a method (addLine) to add an additional blank or comment line to the end of the loaded properties, which will be in the file if you then save the properties to a file.


import java.io.*;
import java.util.*;

/**
 * The CommentedProperties class is an extension of java.util.Properties
 * to allow retention of comment lines and blank (whitespace only) lines
 * in the properties file.
 * 
 * Written for Java version 1.4
 */
public class CommentedProperties extends java.util.Properties {

	/**
	 * Use a Vector to keep a copy of lines that are a comment or 'blank'
	 */
	public Vector lineData = new Vector(0, 1);

	/**
	 * Use a Vector to keep a copy of lines containing a key, i.e. they are a property.
	 */
	public Vector keyData = new Vector(0, 1);

	/**
	 * Load properties from the specified InputStream. 
	 * Overload the load method in Properties so we can keep comment and blank lines.
	 * @param   inStream   The InputStream to read.
	 */
	public void load(InputStream inStream) throws IOException
	{
		// The spec says that the file must be encoded using ISO-8859-1.
		BufferedReader reader =
		new BufferedReader(new InputStreamReader(inStream, "ISO-8859-1"));
		String line;

		while ((line = reader.readLine()) != null) {
			char c = 0;
			int pos = 0;
			// Leading whitespaces must be deleted first.
			while ( pos < line.length()
					&& Character.isWhitespace(c = line.charAt(pos))) {
				pos++;
			}

			// If empty line or begins with a comment character, save this line
			// in lineData and save a "" in keyData.
			if (	(line.length() - pos) == 0
					|| line.charAt(pos) == '#' || line.charAt(pos) == '!') {
				lineData.add(line);
				keyData.add("");
				continue;
			}

			// The characters up to the next Whitespace, ':', or '='
			// describe the key.  But look for escape sequences.
			// Try to short-circuit when there is no escape char.
			int start = pos;
			boolean needsEscape = line.indexOf('\\', pos) != -1;
			StringBuffer key = needsEscape ? new StringBuffer() : null;

			while ( pos < line.length()
					&& ! Character.isWhitespace(c = line.charAt(pos++))
					&& c != '=' && c != ':') {
				if (needsEscape && c == '\\') {
					if (pos == line.length()) {
						// The line continues on the next line.  If there
						// is no next line, just treat it as a key with an
						// empty value.
						line = reader.readLine();
						if (line == null)
							line = "";
						pos = 0;
						while ( pos < line.length()
								&& Character.isWhitespace(c = line.charAt(pos)))
							pos++;
					} else {
						c = line.charAt(pos++);
						switch (c) {
							case 'n':
								key.append('\n');
								break;
							case 't':
								key.append('\t');
								break;
							case 'r':
								key.append('\r');
								break;
							case 'u':
								if (pos + 4 <= line.length()) {
									char uni = (char) Integer.parseInt
											   (line.substring(pos, pos + 4), 16);
									key.append(uni);
									pos += 4;
								}   // else throw exception?
								break;
							default:
								key.append(c);
								break;
						}
					}
				} else if (needsEscape)
					key.append(c);
			}

			boolean isDelim = (c == ':' || c == '=');

			String keyString;
			if (needsEscape)
				keyString = key.toString();
			else if (isDelim || Character.isWhitespace(c))
				keyString = line.substring(start, pos - 1);
			else
				keyString = line.substring(start, pos);

			while ( pos < line.length()
					&& Character.isWhitespace(c = line.charAt(pos)))
				pos++;

			if (! isDelim && (c == ':' || c == '=')) {
				pos++;
				while ( pos < line.length()
						&& Character.isWhitespace(c = line.charAt(pos)))
					pos++;
			}

			// Short-circuit if no escape chars found.
			if (!needsEscape) {
				put(keyString, line.substring(pos));
				// Save a "" in lineData and save this
				// keyString in keyData.
				lineData.add("");
				keyData.add(keyString);
				continue;
			}

			// Escape char found so iterate through the rest of the line.
			StringBuffer element = new StringBuffer(line.length() - pos);
			while (pos < line.length()) {
				c = line.charAt(pos++);
				if (c == '\\') {
					if (pos == line.length()) {
						// The line continues on the next line.
						line = reader.readLine();

						// We might have seen a backslash at the end of
						// the file.  The JDK ignores the backslash in
						// this case, so we follow for compatibility.
						if (line == null)
							break;

						pos = 0;
						while ( pos < line.length()
								&& Character.isWhitespace(c = line.charAt(pos)))
							pos++;
						element.ensureCapacity(line.length() - pos +
											   element.length());
					} else {
						c = line.charAt(pos++);
						switch (c) {
							case 'n':
								element.append('\n');
								break;
							case 't':
								element.append('\t');
								break;
							case 'r':
								element.append('\r');
								break;
							case 'u':
								if (pos + 4 <= line.length()) {
									char uni = (char) Integer.parseInt
											   (line.substring(pos, pos + 4), 16);
									element.append(uni);
									pos += 4;
								}   // else throw exception?
								break;
							default:
								element.append(c);
								break;
						}
					}
				} else
					element.append(c);
			}
			put(keyString, element.toString());
			// Save a "" in lineData and save this
			// keyString in keyData.
			lineData.add("");
			keyData.add(keyString);
		}
	}

	/**
	 * Write the properties to the specified OutputStream.
	 * 
	 * Overloads the store method in Properties so we can put back comment	
	 * and blank lines.													  
	 * 
	 * @param out	The OutputStream to write to.
	 * @param header Ignored, here for compatability w/ Properties.
	 * 
	 * @exception IOException
	 */
	public void store(OutputStream out, String header) throws IOException
	{
		// The spec says that the file must be encoded using ISO-8859-1.
		PrintWriter writer
		= new PrintWriter(new OutputStreamWriter(out, "ISO-8859-1"));

		// We ignore the header, because if we prepend a commented header
		// then read it back in it is now a comment, which will be saved
		// and then when we write again we would prepend Another header...

		String line;
		String key;
		StringBuffer s = new StringBuffer ();

		for (int i=0; i<lineData.size(); i++) {
			line = (String) lineData.get(i);
			key = (String) keyData.get(i);
			if (key.length() > 0) {  // This is a 'property' line, so rebuild it
				formatForOutput (key, s, true);
				s.append ('=');
				formatForOutput ((String) get(key), s, false);
				writer.println (s);
			} else {  // was a blank or comment line, so just restore it
				writer.println (line);
			}
		} 
		writer.flush ();
	}

	/**
	 * Need this method from Properties because original code has StringBuilder,
	 * which is an element of Java 1.5, used StringBuffer instead (because
	 * this code was written for Java 1.4)
	 * 
	 * @param str	- the string to format
	 * @param buffer - buffer to hold the string
	 * @param key	- true if str the key is formatted, false if the value is formatted
	 */
	private void formatForOutput(String str, StringBuffer buffer, boolean key)
	{
		if (key) {
			buffer.setLength(0);
			buffer.ensureCapacity(str.length());
		} else
			buffer.ensureCapacity(buffer.length() + str.length());
		boolean head = true;
		int size = str.length();
		for (int i = 0; i < size; i++) {
			char c = str.charAt(i);
			switch (c) {
				case '\n':
					buffer.append("\\n");
					break;
				case '\r':
					buffer.append("\\r");
					break;
				case '\t':
					buffer.append("\\t");
					break;
				case ' ':
					buffer.append(head ? "\\ " : " ");
					break;
				case '\\':
				case '!':
				case '#':
				case '=':
				case ':':
					buffer.append('\\').append(c);
					break;
				default:
					if (c < ' ' || c > '~') {
						String hex = Integer.toHexString(c);
						buffer.append("\\u0000".substring(0, 6 - hex.length()));
						buffer.append(hex);
					} else
						buffer.append(c);
			}
			if (c != ' ')
				head = key;
		}
	}

	/**
	 * Add a Property to the end of the CommentedProperties. 
	 * 
	 * @param   keyString	 The Property key.
	 * @param   value		 The value of this Property.
	 */
	public void add(String keyString, String value)
	{
		put(keyString, value);
		lineData.add("");
		keyData.add(keyString);
	}

	/**
	 * Add a comment or blank line or comment to the end of the CommentedProperties. 
	 * 
	 * @param   line The string to add to the end, make sure this is a comment
	 *			   or a 'whitespace' line.
	 */
	public void addLine(String line)
	{
		lineData.add(line);
		keyData.add("");
	}
}



What is the licensing on this code? In other words, may I use it for a work-related project which is not open source?

Thanks.
Was This Post Helpful? 0
  • +
  • -

#14 waltf  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 10
  • Joined: 23-December 09

Re: Java Code to modify properties file and preserve comments

Posted 14 January 2010 - 03:26 PM

Quote

What is the licensing on this code? In other words, may I use it for a work-related project which is not open source?

Thanks.


Sure, no problem.

waltf
Was This Post Helpful? 0
  • +
  • -

#15 Capoeira  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-January 10

Re: Java Code to modify properties file and preserve comments

Posted 28 January 2010 - 04:00 AM

Hi waltf,

That was a really neat solution.
Would you mind on giving me a help? I am getting a "Null Pointer" in the formatForOutput method when it is being called to include the value of the key. I tried to find where you were saving that value but, could not find.
Was This Post Helpful? 0
  • +
  • -

#16 waltf  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 10
  • Joined: 23-December 09

Re: Java Code to modify properties file and preserve comments

Posted 02 February 2010 - 06:12 AM

View PostCapoeira, on 28 January 2010 - 03:00 AM, said:

Hi waltf,

That was a really neat solution.
Would you mind on giving me a help? I am getting a "Null Pointer" in the formatForOutput method when it is being called to include the value of the key. I tried to find where you were saving that value but, could not find.


The formatForOutput method is called twice from the store method, once to output the key itself:

formatForOutput (key, s, true);

and again to output the key's value:

formatForOutput ((String) get(key), s, false);

I would check that the '(String) get(key)' is not returning null, change it to something like this:

String keyValue = (String) get(key);
if (keyValue == null) {
keyValue = "";
}
formatForOutput ((String) get(key), s, false);

In the above if there is no value for the key then it will use an empty string as the value...

-waltf
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2