• Post Reply Bookmark Topic Watch Topic
  • New Topic

create property fiel with Map  RSS feed

 
jacob deiter
Ranch Hand
Posts: 588
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to create a file with Key and values,


I prefer the approach

Map <k,v> o=new HAshmap<k,v>();

o.add("key1","value1");
o.add("key2","value2");
o.add("key3","value3");
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You had a question?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know exactly what your question is, but you might be interested in the java.util.Properties class.

It works like a map, but also contains methods to load and save the keys and values from and to a file. For example:

Lookup class java.util.Properties in the Java API documentation.
 
jacob deiter
Ranch Hand
Posts: 588
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i forgot to add the my question,can i use the above approach for property file creation ??
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose so, but why do that when the Properties class already contains all the functionality you probably need?
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob:

As Jesper suggested, you probably want to look at the java.util.Properties class. It has methods for saving to a .properties file in straight text or XML format. One of the (minor) downsides of Properties is that it extends Hashtable (rather than just wrapping it), so you have direct access to its methods.

John.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class Properties is an old class, it already existed in Java 1.0, before the time when the collections framework was added to Java (which was in Java 1.2) - that's why it extends the legacy collection class Hashtable. In the current version of Java, it also implements Map<Object, Object>, as the documentation says.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper:

Yes, that's true. However, Map<Object, Object> doesn't really help with type safety. If the designers had chosen to make the Properties class wrap Hashtable instead of extending it, then when generics came along, they could have changed Properties to wrap a Map<String, String>, preserving type safety. Obviously, hindsight is 20/20, but having Hashtable's methods exposed does expose a risk.

John.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!