DotProperties Build Status

Reads and writes Java .properties files like a champ.

  • Intuitive, Hash-like access. Anywhere it makes sense to act like a Hash, it acts like a Hash.
  • Won't clobber comments and blank lines (unless you want to).
  • Will preserve original delimiters for each value (unless you normalize them).
  • Supports all the delimiters (whitespace, =, :).
  • Supports both comment prefixes (#, !).
  • Supports expansion of inline ${property} references.

Installation

Add this line to your application's Gemfile:

gem 'dot_properties'

And then execute:

$ bundle

Or install it yourself as:

$ gem install dot_properties

Usage

require 'dot_properties'

# Load a .properties file
props = DotProperties.load('sample.properties')

# Get a value
props['foo']

# Set a value
props['foo'] = 'bar'

# Convert key/value pairs to a hash
props.to_h

# Or just let it act like a hash 
props.each_pair { |key,value| puts "#{key} :: #{value}" }

# Remove all comments/blanks/both
props.strip_comments!
props.strip_blanks!
props.compact!

# Write a .properties file
File.open('output.properties','w') { |out| out.write(props.to_s) }

See the spec tests and fixture data for more examples.

Known Issues

  • Multiline values will be converted to single line on output

History

  • 0.1.3 - Explicitly require 'forwardable' (jruby / mri2.x compat) (from @billdueber)
  • 0.1.2 - Improved escaping and Unicode (\uXXXX) support
  • 0.1.1 - Fix mishandled keys with leading whitespace (#1)
  • 0.1.0 - Initial release

Copyright (c) 2013 Michael B. Klein. See LICENSE.txt for further details.