解决java 命令行乱码的问题
虚拟机参数加上
-Dfile.encoding=GBK -Ddefault.client.encoding=GBK -Duser.language=zh -Duser.region=CN
补充:java执行cmd命令,返回结果中文乱码问题解决
public static void main(String[] args) { try { // 执行ping命令 Process process = Runtime.getRuntime().exec('cmd /c e:&dir'); BufferedReader br = new BufferedReader( new InputStreamReader( process.getInputStream(), Charset.forName('GBK') )); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } }
我的代码
package net.bigwrok;import java.io.BufferedReader;import java.io.InputStreamReader;import java.nio.charset.Charset;public class Test2_net { public static void main(String[] args) {BufferedReader br = null; try { Process p = Runtime.getRuntime().exec('ping 127.0.0.1'); br = new BufferedReader(new InputStreamReader(p.getInputStream(),Charset.forName('GBK'))); String line = null; StringBuilder sb=new StringBuilder(); while ((line = br.readLine()) != null) {sb.append(line+'n'); } System.out.println(sb.toString()); } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) {try { br.close();} catch (Exception e) { e.printStackTrace();} } } }}
结果
以上为个人经验,希望能给大家一个参考,也希望大家多多支持好吧啦网。如有错误或未考虑完全的地方,望不吝赐教。
相关文章: