Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 205
Maozturk Avatar asked May 01 '11 17:05

Maozturk


People also ask

What is the difference between BufferedReader and InputStreamReader in Java?

BufferedReader reads a couple of characters from the Input Stream and stores them in a buffer. InputStreamReader reads only one character from the input stream and the remaining characters still remain in the streams hence There is no buffer in this case.

Do I need to close InputStreamReader Java?

read(); // throws exception: stream is closed. Therefore, if you close the Reader, you don't need to also close the InputStream.

How do I convert InputStreamReader to InputStream?

And here is an example of using InputStreamReader (obviously with the help of InputStream): InputStream inputStream = new FileInputStream("c:\\data\\input. txt"); Reader reader = new InputStreamReader(inputStream); int data = reader. read(); while(data !=


1 Answers

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.

like image 169
Maozturk Avatar answered Sep 21 '22 01:09

Maozturk