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.
UnsupportedEncodingExceptionwould through exception if that encoding is not supported .UTF-8,UTF-16are both supported & valid encodings.CharsetDecoder. See also theCodingErrorActionclass: the default for all classes is toCodingErrorAction.REPLACEand notREPORTCharsetDecoderallows you to detect encoding errors... Not even aReaderclass. If you want that you'd have to create your ownReaderimplementation! That kind of sucks.