1

If I pass in a UTF-16 encoded file to the following code then will I get an UnsupportedEncodingException?

    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));
        String ip;
        while ((ip = br.readLine()) != null){
            //do something
        }
    } catch (UnsupportedEncodingException use) { 
        //when can I expect an exception?
    }

I have tried this with a UTF-16 file but I am not getting any exception. The reader somehow tries to read all the characters which causes it to read more line than expected. For example in a sample file with 3 lines the reader reads 5 lines, 2 of which are empty lines.

4
  • 2
    UnsupportedEncodingException would through exception if that encoding is not supported . UTF-8,UTF-16 are both supported & valid encodings. Commented May 9, 2014 at 11:24
  • 1
    If you want to detect encoding errors you cannot use the "standard" Java classes; you have to go through a CharsetDecoder. See also the CodingErrorAction class: the default for all classes is to CodingErrorAction.REPLACE and not REPORT Commented May 9, 2014 at 11:29
  • 1
    Note that curiously enough, no class in the JDK apart from CharsetDecoder allows you to detect encoding errors... Not even a Reader class. If you want that you'd have to create your own Reader implementation! That kind of sucks. Commented May 9, 2014 at 11:33
  • thanks a lot guys and yes +1 for figuring out that I am looking for CharsetDecoder Commented May 9, 2014 at 11:39

1 Answer 1

3

UnsupportedEncodingException is only thrown if the name of the charset you pass to the Charset.forName() is not supported. It does not relate to the content of the stream (the Exception is declared to be thrown by the Charset.forName() not by BufferedReader or InputStreamReader classes).

Sign up to request clarification or add additional context in comments.

2 Comments

I can accept this in 10min. So, what should I do to find out if I have got the correctly encoded stream?
Detecting file content encoding: stackoverflow.com/questions/499010/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.