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);
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.
read(); // throws exception: stream is closed. Therefore, if you close the Reader, you don't need to also close the 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 !=
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With