Search code examples
javacharacter-encodinginputstreaminputstreamreader

Java inputStreamReader Charset


I want to ping a target IP address and receive a response. To achieve this, I'm using windows command line in Java with runtime.exec method and process class. I'm getting the response using inputStreamReader.

My default charset is windows-1254, it's Turkish. When I receive it, the response contains Turkish characters but Turkish characters are not displayed correctly in the console.

I want to get a numeric value from the response I get but the value that I am searching for contains some Turkish characters, so when I look it up, I can't find it.

The codes are below, what I need to know is how to get the Turkish characters visible here:

runtime = Runtime.getRuntime();
process = runtime.exec(pingCommand);

BufferedReader bReader = new BufferedReader(
        new InputStreamReader(process.getInputStream(), "UTF8"));

String inputLine;
while ((inputLine = bReader.readLine()) != null) {
    pingResult += inputLine;
}

bReader.close();
process.destroy();

System.out.println(pingResult);

Solution

  • In order to fix this, checking the charset that your current operating system uses on its command line and getting the data compatible with this charset is necessary.

    I figured out that charset for Turkish XP command line is CP857, and when you edit the code like below, the problem is solved.

    BufferedReader bReader = new BufferedReader(
            new InputStreamReader(process.getInputStream(), "CP857"));
    

    Thx for your help.

    Note : You can learn your default command line charset by "chcp" command.