Home > General, Tech > Reading and Writing Java Property Files With Ruby

Reading and Writing Java Property Files With Ruby

Created a simple Class in ruby to help reading and writing property files

class JavaProps
attr :file, :properties

#Takes a file and loads the properties in that file
def initialize file
@file = file
@properties = {}
IO.foreach(file) do |line|
@properties[$1.strip] = $2 if line =~ /([^=]*)=(.*)\/\/(.*)/ || line =~ /([^=]*)=(.*)/
end
end

#Helpfull to string
def to_s
output = “File Name
#{@file} \n”
@properties.each {|key,value| output += ” #{key}= #{value} \n” }
output
end

#Write a property
def write_property (key,value)
@properties[key] = value
end

#Save the properties back to file
def save
file = File.new(@file,”w+”)
@properties.each {|key,value| file.puts “#{key}=#{value}\n” }
end

end

About these ads
Categories: General, Tech
  1. Monkey Magic
    October 30, 2008 at 4:09 pm

    What about escaping?

  2. Tiago Romero Garcia
    January 27, 2009 at 4:08 pm

    Hi! I am using your class in a project, and I would like to post it in my blog. I will put a link to here. Would you approve this? I hope to do so :)

    Thanks!

  3. Tiago Romero Garcia
    January 27, 2009 at 4:09 pm

    By the way, my blog is: http://tiagoromero.wordpress.com/

    If you agree, can you please post your response for me there?

    Thanks again!

  4. Flug USA
    March 25, 2009 at 7:15 am

    Good work
    As a java developper , I keep my eye on other languages that can play a role in java-based systems, among them ruby.That’s why I’m really interested in learning Ruby. I found your code really helpful .
    I’m expecting more useful RUBY tips from you for java developpers

  5. Markie
    January 18, 2010 at 5:40 pm

    Hi, if anyone interested I found this extended version based on the above.

    http://github.com/axic/rsnippets/blob/master/javaproperties.rb

  6. Tammo
    March 31, 2010 at 6:54 am

    I improved the regular expression a little. And now it uses # for comments, which is the normal way in properties files.

    properties[$1.strip] = $2.strip if line =~ /([^=#]+)=([^=#]+)/

  7. November 7, 2011 at 12:45 pm

    a little improvement would be to alter your reader/writer methods to have array-like syntax:

    class Props
    def initialize(filename = nil)
    @props = {}
    if (filename) then
    File.open(filename).
    select { |line| not line=~/^[ \t]*(#.+)*$/ }. # ignore comments and blank lines
    each { |line|
    (k,v) = line.chomp.split(‘=’,2)
    @props[k] = v
    }
    end
    end

    def []=(k,v)
    @props[k]=v
    end

    def [](k)
    @props[k]
    end

    def to_s
    @props.map{ |k,v| “#{k}=#{v}”}.join(“\n”)
    end
    end

    p = Props.new(‘test.properties’)
    puts p['first']
    puts p.to_s

    # cheers

  8. June 11, 2013 at 9:56 pm

    Thanks for some other informative web site. The place else
    may just I am getting that kind of info written in such an ideal manner?

    I have a project that I am just now running on, and I have
    been on the glance out for such information.

  9. September 12, 2013 at 1:34 am

    Its such as you read my mind! You seem to know a lot approximately this, such
    as you wrote the ebook in it or something. I feel
    that you just could do with a few p.c. to pressure the message house a bit, however instead
    of that, this is great blog. An excellent read.

    I will definitely be back.

  10. March 1, 2014 at 5:29 pm

    What’s Going down i am new to this, I stumbled upon this I have discovered
    It positively helpful and it has helped me out loads.
    I am hoping to give a contribution & assist other users like its aided me.
    Good job.

  1. February 10, 2009 at 8:09 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: