Java inputStreamReader 字符集

发布于 2024-11-04 02:45:07 字数 658 浏览 6 评论 0原文

我想 ping 目标 IP 地址并收到响应。为了实现这一点,我在 Java 中使用 Windows 命令行以及 runtime.exec 方法和进程类。我正在使用 inputStreamReader 获取响应。

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

我想从得到的响应中获取一个数值,但我正在搜索的值包含一些土耳其字符,所以当我查找它时,我找不到它。

代码如下,我需要知道的是如何让土耳其字符在这里可见:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

心欲静而疯不止 2024-11-11 02:45:08

为了解决这个问题,需要检查当前操作系统在命令行上使用的字符集并获取与该字符集兼容的数据。

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

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

谢谢你的帮助。

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

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.

蔚蓝源自深海 2024-11-11 02:45:08

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

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

一下可能会有效

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

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

That MIGHT work

迟到的我 2024-11-11 02:45:08

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

下面是一个完整的程序,您可以使用它来执行此操作。注意:在 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;
  }

}

以下是我在 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

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

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:

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".

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

$ 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

阪姬 2024-11-11 02:45:08

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

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.

惜醉颜 2024-11-11 02:45:08

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

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

看海 2024-11-11 02:45:08

尝试这个命令行命令:

chcp

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

Try this command line command:

chcp

My command line answered 866 so I used CP866

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文