最近在学习如何在 Java 程序中调用系统中的命令,程序如下: public static void main(String[] args) { String cmd="ipconfig"; Runtime run=Runtime.getRuntime(); try { Process process=run.exec(cmd); InputStream in=process.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String s = null; while ((s = reader.readLine()) != null) { System.out.println(s); } in.close(); process.waitFor(); }catch (IOException e){ e.printStackTrace(); }catch (InterruptedException e){ e.printStackTrace(); }
}
在 IDEA 中直接运行的时候,汉字部分显示为乱码,但在命令行中运行该程序却能正确显示汉字。 应该是两个环境的字符编码不同导致,该如何修改才能让 IDEA 能够正确显示汉字?