1

When using

new BufferedReader(new InputStreamReader(inputStream))

and then mapping each line like

 String(bufferedReader.readLine().getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);

for some cases doesnt map as expected

when using

new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.ISO_8859_1))
it sees characters as expected

I cannot use second second method with providing the charset to InputStreamReader. Any idea why it wouldn't work?

Progman
  • 16,827
  • 6
  • 33
  • 48

1 Answers1

5

Thats because InputStreamReader assumes the default charset by default (per the Javadocs).

You should specify the charset of the file you're reading as you did in the second example like this:

new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.ISO_8859_1))

The only problem is that it may not be possible to know what encoding your file has. For example in Windows you could assume ISO, but there may also be UTF-8 files.

Some solutions to this problem are discussed here: Java : How to determine the correct charset encoding of a stream

Using external libraries.

AminM
  • 822
  • 4
  • 11