As you will know, InputStreamReader
will read the provided InputStream
and decode its bytes into characters. If no charset
is specified, it will use the default charset.
We can check this default charset with java.nio.charset.Charset.defaultCharset().displayName()
.
Case 1. My Windows CMD uses cp850
, but Java reports windows-1252
. It can be proven typing the character ó
and System.in.read()
will report 162
, as expected. The InputStreamReader
, though, will fail to decode it, as it expects to be running windows-1252
, showing ¢
(this is the 162nd windows-1252
character).
Case 2. In Windows, my Netbeans integrated terminal uses windows-1252
, but Java reports UTF-8
. Again, it can be proven typing the character ó
and System.in.read()
will report 243
, as expected. The InputStreamReader
, though, will fail to decode it, as it expects to be running UTF-8
, showing �
(code 65533
).
Case 3. My Debian machine uses UTF-8
everywhere, in both GNOME and Netbeans terminals. When typing the character ó
, System.in.read()
will report two bytes, 195
and 161
, which correspond to the UTF-8
representation of that character. The InputStreamReader
will show ó
as expected.
What I want? Is there a way to correctly detect the actual charset used so I can read characters from the command line (in Windows CMD and Netbeans in Windows) without any special case?
Thank you very much.
The B plan: Case 2 can be solved by changing Netbeans file encoding to UTF-8 (and it will handle UTF-8 files too, which is what an IDE should do in 2019). Case 1 could be solved by changing the codepage to UTF-8, but I have not been able to make that work.
You may use the following program to test these cases. Enter the same characters twice and compare the output.
import java.io.*;
import java.nio.charset.Charset;
public class Prova2 {
public static void main(String[] args) throws Exception {
int b;
System.out.println("Charset.defaultCharset: " + Charset.defaultCharset().displayName());
System.out.println("I will read the next bytes: ");
while ((b = System.in.read()) != '\n') {
System.out.println("I have read this byte: " + b + " (" + (char) b + ")");
}
System.out.println("I will read the next chars: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while ((b = br.read()) != '\n') {
System.out.println("I have read this char: " + b + " (" + (char) b + ")");
}
System.out.println("Thank you.");
}
}