This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Properties megafile (1MTR)  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, developed project but want to create properties file where I cN STORE ALL properties project-wide....

Path to properties:: /properties.conf
Fuckance: dynamically built on compile

Within each file where I need properties::


Of course param name and value are taken from that file. But its not working. Why?
 
Saloon Keeper
Posts: 10206
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first of all that's not valid Java code. Secondly, System.getProperty() does not get properties from a file properties.conf, as far as I'm aware.

You will want to include a default properties file as a resource in your JAR and load it with Class.getResourceAsStream(). Then, if you want the user to be able to change properties, load an additional properties file from disk. It looks like this:

For this to work, the application.properties file containing the default properties must be in the same package as the Application class, although the packages can be in different source folders as long as both folders are in the source path when you build the application.
 
Bartender
Posts: 20721
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For complex systems, it's not uncommon to store system-wide property overrides in "/etc/myapplication.conf" or as a file in "/etc/myapplication/" on Unix-like systems. Windows, alas, doesn't have an equivalent that I can think of and the Registry isn't well-suited as an OS-indpenendent property source.

For an extra level of customization, you can make a properties file based off the user.home directory. The popular approach these days is to place the user properties file in the user's $HOME/.config directory (note the dot - it's a hidden directory). This is a refinement on the older style where every app's properties file was anchored directly in the user.home directory itself. Once it became normal to have dozens of apps, it tended to clutter the home directory, so now we have .config. Again, this is Linux/MacOS, but you could make a .config directory under the user.home in Windows and few would complain, I think.

Since Properties can stack, you can arrange user preferences to override system preferences which override built-in preferences on an item-by-item basis.
 
Wojtek Eu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding what Stephan van Hulst wrote:
Many thanks, but why encoding to/as UTF8?
Cannot be - lets say - Latin2 or whatever?
 
Marshal
Posts: 64473
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That way the file path is written suggests the OP is using a Unix‑like system, where UTF‑8 is the commonest encoding.
 
Wojtek Eu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That way the file path is written suggests the OP is using a Unix‑like system, where UTF‑8 is the commonest encoding.


Correct macOS to be precise
 
Stephan van Hulst
Saloon Keeper
Posts: 10206
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because UTF-8 is the one true encoding. UTF-8 can encode all Unicode characters, including eastern European, Arabic, Asian and lesser known scripts, as well as some symbols and emoji's.

When you always use UTF-8, you can be sure that you don't have to rewrite your program when suddenly the need arises to store characters that can't be encoded in Latin-1 or Latin-2.

There are other encodings that can encode all Unicode characters, such as UTF-16, but they are simply inferior to UTF-8. See  UTF-8 Everywhere for an explanation why.

 
Marshal
Posts: 24461
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Because UTF-8 is the one true encoding.



Absolutely 100% true. However it's worth mentioning that you have to use the load(Reader) method of Properties, as you did in your code, to be able to use UTF-8. If you use the load(InputStream) method then it annoyingly defaults to ISO-8859-1.
 
Stephan van Hulst
Saloon Keeper
Posts: 10206
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always roll my eyes when an API that works with textual data, such as XML processors, supply convenience methods that accept InputStream. Making assumptions about the encoding is what causes confused programmers and heaps of bugs.
 
Tim Holloway
Bartender
Posts: 20721
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I always roll my eyes when an API that works with textual data, such as XML processors, supply convenience methods that accept InputStream. Making assumptions about the encoding is what causes confused programmers and heaps of bugs.



Well, in the case of XML processing, the encoding is specified in the header and the processor is supposed to reject bad input. Not just illegal characters, but generally stuff that's syntactically invalid and stuff that's semantically invalid. To the point where it can get really frustrating trying to persuade it that you don't want the XML cross-checked against a DTD or schema.

Other systems, of course, have other methods, some more rigorous than others.
 
Stephan van Hulst
Saloon Keeper
Posts: 10206
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose an exception can be made for formats that have explicit constraints on the encoding. You're right, XML was a poor example on my part.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!