Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading utf-8 properties files  RSS feed

 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,



The above code works fine for english but when I try to read property files UTF-8 values then it displaying garbage.

Please advise how can I read UTF-8 values from properties files

Best regards
 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of using the load(InputStream) method, which as you can see from the API documentation treats the file as if it were encoded in ISO-8859-1, you could use the load(Reader) method. Use a Reader which encodes the input as UTF-8; the easiest way to do that is via an InputStreamReader which wraps your FileInputStream and specifies UTF-8 as the charset.
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The code read UTF-8 properties file without any error but problem is I have to put *.properties file in class path. When I put property file in other folder then its not working.

Please advise any fix for path other then class path

Best regards
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It requires the properties file to be in your classpath because you are reading it from that location in the below code.

You could instead provide the path where you want to keep your properties file.
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Amit for your favorable reply

I did as Its compiling the code but throwing when I ran it

Please advise
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also tried as:


but throwing runtime error
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any help please
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Ghorpade wrote:It requires the properties file to be in your classpath because you are reading it from that location in the below code.

You could instead provide the path where you want to keep your properties file.


fileName is getting both filePath and fileName
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read Paul's reply again. He suggest wrapping the FileInputStream you have already created in an InputStreamReader with a character enoding of UTF-8. He didn't mention using getResource().
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all of you for giving me your precious time.

Now I reshape my code as below and it worked fine



Thanks again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!