Properties from *.ini file only put as String (not as Int)?

P

pete sinner

I would like to write resp. read parameters to and from an *.ini file through Properties
feature.

As far as I can see I can treat parameters in such *.ini files only as Strings.
I cannot put e.g. an "int" value directly as in:

p.put("width", this.getWidth())";

This yields an compiler error.

Do I really have to convert intergers before put them?
Or is there an option to enable put directly?

Pete
 
K

Knute Johnson

pete said:
I would like to write resp. read parameters to and from an *.ini file through Properties
feature.

As far as I can see I can treat parameters in such *.ini files only as Strings.
I cannot put e.g. an "int" value directly as in:

p.put("width", this.getWidth())";

This yields an compiler error.

Do I really have to convert intergers before put them?
Yes.

Or is there an option to enable put directly?

There are other methods for writing different types to files but not
with Properties.
 
R

Roedy Green

There are other methods for writing different types to files but not
with Properties.

I wrote an extended properties mechanism that bypasses this problem
and also allows multiple values per key. You may cannibalise it from
the Replicator.

http://mindprod.com/products1.html#REPLICATOR

You don't have to download the whole thing. Just browse the
repository.
--
Roedy Green Canadian Mind Products
http://mindprod.com

If everyone lived the way people do in Vancouver, we would need three more entire planets to support us.
~ Guy Dauncey
 
I

Ian Shef

(e-mail address removed) (pete sinner) wrote in

I would like to write resp. read parameters to and from an *.ini file
through Properties feature.

As far as I can see I can treat parameters in such *.ini files only as
Strings. I cannot put e.g. an "int" value directly as in:
Do I really have to convert intergers before put them?
<snip>
From the javadoc (version 1.6) for Properties:
"The Properties class represents a persistent set of properties. ... Each key
and its corresponding value in the property list is a string."

Yes, you have to convert. It is easy. Instead of

p.put("width", this.getWidth()); // removed superfluous double-quote

use

p.put("width", String.valueOf(this.getWidth()));


Instead of

p.getProperty("width");

use

Integer.valueOf(p.getProperty("width"));


I won't get sidetracked here explaining why using a String literal may be a
poor idea.
 
A

Arne Vajhøj

pete said:
I would like to write resp. read parameters to and from an *.ini file through Properties
feature.

As far as I can see I can treat parameters in such *.ini files only as Strings.
I cannot put e.g. an "int" value directly as in:

p.put("width", this.getWidth())";

This yields an compiler error.

Do I really have to convert intergers before put them?
Or is there an option to enable put directly?

Not in properties.

XMLEncode/XMLDecode and JAXB have type safe alternatives.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top