Java inputStreamReader 字符集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5849990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:06:29  来源:igfitidea点击:

Java inputStreamReader Charset

javacharacter-encodinginputstreaminputstreamreader

提问by Maozturk

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.

我想 ping 目标 IP 地址并接收响应。为了实现这一点,我在 Java 中使用带有 runtime.exec 方法和进程类的 Windows 命令行。我正在使用 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.

我的默认字符集是 windows-1254,它是土耳其语。当我收到它时,响应包含土耳其语字符,但土耳其语字符未在控制台中正确显示。

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);

回答by Maozturk

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.

我发现土耳其语 XP 命令行的字符集是 CP857,当您像下面这样编辑代码时,问题就解决了。

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.

注意:您可以通过“chcp”命令了解您的默认命令行字符集。

回答by jefflunt

If you just need to ping and get the response time, then reading the output from the console might be overkill. There's an easier way so long as you're using Java5 or newer:

如果您只需要 ping 并获得响应时间,那么从控制台读取输出可能有点过头了。只要您使用 Java5 或更新版本,就有一种更简单的方法:

Here is a complete program that you can use to do this. NOTE: On Unix/Linux/Mac OS, you have to run this program under "sudo" in order to get a response from anything other than "localhost".

这是一个完整的程序,您可以使用它来执行此操作。注意:在 Unix/Linux/Mac OS 上,您必须在“sudo”下运行此程序才能从“localhost”以外的任何内容中获得响应。

import java.net.InetAddress;
import java.io.IOException;

class PingTest {

  public static void main(String[] args) {
    try {
      String hostnameOrIP = args[0];
      int timeout = Integer.parseInt(args[1]);
      int pingCount = Integer.parseInt(args[2]);

      System.out.println("Pinging '" + hostnameOrIP + "'");
      for (int i = 0; i < pingCount; i++) {
        System.out.println("Response time: " + getPingResponseTime(hostnameOrIP, timeout));
      }
    } catch (Exception e) {
      System.out.println("Usage: java PingTest <hostname/IP address> <timeout in milliseconds> <number of pings to send>\n");
    }
  }

  static long getPingResponseTime(String hostnameOrIP, int timeout) {
      long startTime = System.currentTimeMillis();

      boolean successfulPing = false;

      try {
        successfulPing = InetAddress.getByName(hostnameOrIP).isReachable(timeout);
      } catch (IOException ioe) {
        successfulPing = false;
      }

      long endTime = System.currentTimeMillis();

      long responseTime = endTime-startTime;

      if (successfulPing == false)
        responseTime = -1;

      return responseTime;
  }

}

Here are the results that I got back when I ran the following on Mac OS (results are in milliseconds):

以下是我在 Mac OS 上运行以下命令时得到的结果(结果以毫秒为单位):

$ sudo java PingTest google.com 5000 5
Pinging 'google.com'
Response time: 419
Response time: 15
Response time: 15
Response time: 16
Response time: 16

Reponse times may vary between runs, but I'm seeing < 20 millisecond response times to most major sites, especially if you run multiple pings

响应时间可能因运行而异,但我看到大多数主要站点的响应时间小于 20 毫秒,尤其是当您运行多个 ping 时

回答by MJB

Are you saying the characters are being retrieved properly but System.out isn't printing them right? System.out is definitely an OLD class. Maybe try

你是说字符被正确检索但 System.out 没有正确打印它们?System.out 绝对是一个老类。也许试试

PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out,"charactersethere"));
out.println(“encoded system”);
out.flush();
out.close();

That MIGHT work

这可能有效

回答by Steve McLeod

System.out(...) - and the Java console - is quite limited in encoding. You can expect basic ASCII characters to work, and that's about all. If you want to use any other encoding, then you should be writing the output to a text file or to a GUI. If you write to the console you'll always have to cope with poor handling of various non-ASCII characters.

System.out(...) - 和 Java 控制台 - 在编码方面非常有限。您可以期待基本的 ASCII 字符能够正常工作,仅此而已。如果您想使用任何其他编码,那么您应该将输出写入文本文件或 GUI。如果您写入控制台,您将始终不得不处理各种非 ASCII 字符的不良处理。

回答by Melih

You need to change your eclipse default encoding setting under preferences. It can be set to UTF8.

您需要在首选项下更改 eclipse 默认编码设置。它可以设置为UTF8.

回答by Aleksandr Sysoev

Try this command line command:

试试这个命令行命令:

chcp

奇普

My command line answered 866 so I used CP866

我的命令行回答 866 所以我用了 CP866