BufferedReader和getInputStream()返回中文字符

时间:2017-12-20 01:19:54

标签: java inputstream bufferedreader optional processbuilder

我正在使用Java运行注册表查询,并读取结果以查看当前计算机上是否安装了Adobe。当我在命令提示符下手动执行注册表查询时,Adobe是其中一个结果,但我的Java方法无法找到它。
我的方法是运行Adobe的注册表查询检查:

public static void checkAdobe() throws IOException, InterruptedException {
    ProcessBuilder builder = new ProcessBuilder("reg", "query",
            "HKEY_LOCAL_MACHINE\\SOFTWARE\\Policies");
    Process reg = builder.start();
    try (BufferedReader output 
            = new BufferedReader(new InputStreamReader(reg.getInputStream()))) {

        Stream<String> keys = output.lines().filter(l -> !l.isEmpty());
        Stream<String> matches = keys.filter(l -> l.contains("\\Adobe"));
        Optional<String> key = matches.findFirst();

        if (key.isPresent()) {
            System.out.println("Found "+key.get());
        } else {
            System.out.println("Can't find");
        }
    }
    reg.waitFor();
}

当我输入:

时,我是否使用本地或管理员命令提示符
> reg query HKEY_LOCAL_MACHINE\SOFTWARE\Policies

我得到HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Adobe作为第一批结果之一 为什么我的程序找不到它?

编辑:
我试过这个(来源 - https://stackoverflow.com/a/30915191/9120489):

public static void main(String[] args) throws IOException, InterruptedException {
    System.out.println(getProcessOutput());
}

public static String getProcessOutput() throws IOException, InterruptedException
{
    ProcessBuilder processBuilder = new ProcessBuilder("reg", "query",
            "HKEY_LOCAL_MACHINE\\SOFTWARE\\Policies");

    processBuilder.redirectErrorStream(true);

    Process process = processBuilder.start();
    StringBuilder processOutput = new StringBuilder();

    try (BufferedReader processOutputReader = new BufferedReader(
            new InputStreamReader(process.getInputStream()));)
    {
        String readLine;

        while ((readLine = processOutputReader.readLine()) != null)
        {
            processOutput.append(readLine + System.lineSeparator());
        }
        process.waitFor();
    }
    return processOutput.toString().trim();
}

但我的结果是ഊ䡋䕙彌佃䅌彍䅃䡉久屓但呗䅒䕜偯汩捩敳屁摯扥ഊ䡋䕙彌佃䅌彍䅃䡉久屓但呗䅒䕜偯汩捩敳屍楣牯獯晴而不是实际的命令输出。

1 个答案:

答案 0 :(得分:0)

最后,我想通了。我需要为BufferedReader InputStreamReader指定字符集:

public void mapvw(View view) {
  // Do related to the view.
}