Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Use BufferedReader with a specific encoding?

  Asked By: Adelina    Date: Jan 16    Category: Java    Views: 10890
  

I want to use BufferedReader's readLine method to read one line at a
time from a text file. This text file may be in any encoding
however, such as UTF-8, Windows-1252, Shift-JIS, etc. I don't need
to auto-detect the encoding. Is there a way to tell BufferedReader
to use a specific encoding?

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Perdita Lopez     Answered On: Jan 16

What about something like this:

BufferedReader br = new BufferedReader(new
InputStreamReader(getClass().getResourceAsStream("yourfile.txt"),
"UTF-8"));

Of course, yourfile.txt must be somewhere at your classpath.

 
Answer #2    Answered By: Faith Hughes     Answered On: Jan 16

Although the getResourceAsStream doesn't
work for me, I changed the statement to:

BufferedReader br = new BufferedReader(new InputStreamReader(new
FileInputStream(fileName), encoding));

and it seems to be working fine.

 
Didn't find what you were looking for? Find more on Use BufferedReader with a specific encoding? Or get search suggestion and latest updates.