aspose file tools
The moose likes Beginning Java and the fly likes create property fiel with Map Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "create property fiel with Map" Watch "create property fiel with Map" New topic
Author

create property fiel with Map

jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
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
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8430

You had a question?


[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
sorry i forgot to add the my question,can i use the above approach for property file creation ??
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

I suppose so, but why do that when the Properties class already contains all the functionality you probably need?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
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
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

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

Joined: Mar 09, 2009
Posts: 600
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: create property fiel with Map
 
Similar Threads
MasterExam question
Generics doubt-How do I read this
Generics are going to be the death of me.
Interface can contain nested Interface ?
more generics