public class Test { public static void main (String[] args) { excuteCommand("ipconfig"); // excuteCommand("ping 10.141.26.50"); } public static void excuteCommand(String command) { Runtime r = Runtime.getRuntime(); Process p; try { p = r.exec(command); BufferedReader br = new BufferedReader(new InputStreamReader(p .getInputStream())); String inline; while ((inline = br.readLine()) != null) { //System.getProperty("inline.encoding"); System.out.println(new String(inline.getBytes("utf-8"),"gbk")); } br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
为测试类,cmd chcp查看编码是936 GBK 需要在eclipse Run Configurations 配置 在(x)=Arguments 的VM arguments输入 -Dfile.encoding=GBK 点击apply
运行时会出现部分乱码 由于jvm使用unicode编码 gbk码 转为utf-8时会出现 码位丢失 暂未找到较好的处理方法