This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

create property fiel with Map  RSS feed

 
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");
 
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?
 
Java Cowboy
Posts: 16084
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 ??
 
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?
 
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
Posts: 16084
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.
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!