18 August 2011

How to solve common problems with load , save , get and set properties in java?

Solution based on java 6 (but it should apply for all version since 1.0)

So you setup your propeties like:

paths.secret.loverslist= C:\\mySecrets\\loverlist.txt

or something similar  but when your application load/save properties and start use them then you discover result are crap and produce bunch of weird characters instead and cause program to work in akward way (Exceptions, JVM crash and other fireworks) and you think:

WTF java ,what's wrong with you ?

most problems are caused by encoding issues* because you need to knows that
java.util.Properties loads properties files using the ISO-8859-1 encoding so your file need to be saved in that format or shit hit the fan.

in rest cases are typo error like path issue (like using \ instead of \\) , so do double check or ask somebody to check for you.

and remeber properties file need be saved using   ISO-8859-1 encoding !!!!

(Source: http://download.oracle.com/javase/6/docs/api/java/util/Properties.html )
  *  from my experience  66% encoding , 33% typo error, 1% others

No comments:

Post a Comment