7/24/2014

java.util.Properties and character encoding

java.util.Properties class (see Java SE 7 Javadoc) by default assumes ISO 8859-1 character encoding in reading and writing. So when a properties file is in other character encoding, you will see strange characters and behaviors.

Properties class has no method or constructor that takes encoding or locale parameter. Fortunately, in Java SE 6, two new methods were added to allow for reading from java.io.Reader and writing to java.io.Writer:

public synchronized void load(Reader reader) throws IOException;

public void list(PrintWriter out)
So you can create Reader or Writer instances with appropriate encoding, and pass them to Properties load or list methods. For example,
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.junit.Test;

public class MyTest {

    @Test
    public void loadPropertiesTest() throws Exception {
        final File jndiPropertiesFile = new File(System.getProperty("user.home"), "tmp/jndi.properties");
        final FileInputStream fileInputStream = new FileInputStream(jndiPropertiesFile);

        // FileReader does not have any constructor that takes encoding, so use InputStreamReader instead
        final InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");

        final Properties jndiProperties = new Properties();
        jndiProperties.load(inputStreamReader);

        //now dump the properties to verify
        jndiProperties.list(System.out);
    }
}
Output from running loadPropertiesTest test:
-- listing properties --
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

24 comments:

GeekOnJava said...

GeekOnJava

Anna said...

Great and Useful Article.

Online Java Training

Java Online Training India

Java Online Course

Java EE course

Java EE training

Best Recommended books for Spring framework

Java Interview Questions








Java Course in Chennai

Java Online Training India

fusionfinancilas said...

I have read your blog its very attractive and impressive.I would like to thank you for the sharing. We are also giving oracle financials Course Online Training,further information visit our site.

wingsio said...

Interesting article! Thank you for sharing them! I hope you will continue to have similar posts to share with everyone!

games draw said...

Great! Many thanks for sharing pianotiles2.com

Alice Taylor said...

great article, I was very impressed about it, wish you would have stayed next share


word-cookies-answers.com

Thu LĂȘ said...

I was very impressed by this post, this site has always been pleasant news. Thank you very much for such an interesting post. Keep working, great job! In my free time, I like play game: douchebagworkout2.org. What about you?

Games 2 Girls said...

This is extremely helpful info!! Very good work. Everything is very interesting to learn and easy to understood. Thank you for giving information.
www.baixar-whatsapp.com.br

ngocanhng said...

I like your all post. You have done really good work. Thank you for the information you provide, it helped me a lot. I hope to have many more entries or so from you.
Very interesting blog.
minecraft2.com.br

Unknown said...


Thanks for sharing this good blog.This is very important and imformative blog for Java . very interesting and useful for students
Java Online Training Bangalore

bloxorz said...

Thank you for your analysis. It is new and different aspects I know before. Looking forward to reading more of your posts

happy wheels said...

I was already using the fork attribute. I added the memory attibutes and it worked like a champ!

DJ PRASATH said...

Thanks for your post. This is excellent information. The list of your blogs is very helpful for those who want to learn, It is amazing!!! You have been helping many application.
best selenium training in chennai | best selenium training institute in chennai

Krystal said...

Nice post. It is really interesting. Thanks for sharing the post!
Domain Registration Company in Singapore
Website design Singapore
Buy SSL certificate Singapore
Business email hosting service Singapore

Laxman S said...

I’m really impressed with your article, such great & usefull knowledge you mentioned here. Thank you for sharing such a good and useful information here in the blog
Kindly visit us @
SATHYA TECHNOSOFT (I) PVT LTD
Social Media Marketing Company | SMO Company India
Social Media Marketing Packages in India
PPC Campaign Price | Google Adwords Pricing in india
PPC Company in India | PPC Services India
Google Adwords company in India | Google Adwords Services in India
Best SEO Company in india | SEO Services in India
Bulk SMS Service India | Bulk SMS India

Sneha Sharma said...

Why Sri Yantra is one of the best Yantra for Money? Follow this link https://thebhakti.com/shri-yantra/ to know the Benefits of Shree Yantra, Shree Yantra Properties and learn more about Shree yantra ...

it's Sri yantra
it's Sri yantra mantra
it's Shree yantra online
it's where to place sri yantra
it's What is the Sri Yantra?
it's What is the meaning of a Yantra?

Sathya said...

Interesting information and attractive.This blog is really rocking... Yes, the post is very interesting and I really like it.I never seen articles like this. I meant it's so knowledgeable, informative, and good looking site. I appreciate your hard work. Good job.
Kindly visit us @
Sathya Online Shopping
Buy AC Online | AC Online Shopping | AC Price | Buy Air Conditioner Online
Inverter Split AC | Best Inverter AC | Split AC Price | Buy Split AC Online
Smart LED TV | Smart TV Price | LED TV Online | Buy LED TV Online
Laptop Online | Laptop Price | Buy Laptop Online | Best Laptop
Buy HD LED TV
Buy Ultra HD TV Online
Buy HD Ready LED TV
Buy Mobile Online

Clipping Path said...

Awesome post.Thanks for sharing

manisha said...

Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your blog? My blog is in the same niche as yours, and my users would benefit from some of the information you provide here. Please let me know if this ok with you. Thank you.

apple iphone service center in chennai | apple ipad service center in chennai | iWatch service center chennai | apple iphone service center in chennai

sathya shri said...

I love the blog. Great post. It is very true, people must learn how to learn before they can learn. lol i know it sounds funny but its very true. . .
DevOps Training in Bangalore

DevOps Training in Bangalore

DevOps Training in Bangalore

DevOps Training in Marathahalli

DevOps Training in Pune

DevOps Online Training-gangboard

naveen said...

I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favourites blog site list and will be checking back soon.
DevOps Training in chennai

DevOps Training in chennai

DevOps Training in chennai

DevOps Training in Velachery

DevOps Training in Tambaram

DevOps Training in sholinganallur

DevOps Training in Annanagar

OLIVIA H said...

Awesome to read this blog.
apple iphone service center in chennai | apple ipad service center in chennai | apple iphone service center in chennai

Indonesian Training Courses said...

lampungservice.com
serviceiphonebandarlampung.blogspot.com
youtubelampung.blogspot.com
bimbellampung.blogspot.com
bateraitanam.blogspot.com

rohini said...

Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
Mobile service center in chennai | iphone service center in chennai | ipod battery replacement in chennai | ipad display replacement in chennai | 100% genuine mobile parts | Mobile phone display replacement in chennai | Mobile display replacement in chennai | Mobile service center in chennai | Mobile display replacement in chennai | Tablet battery replacement service in chennai | Latop service center in chennai | Mobile service center in chennai