虚拟机参数加上
1
|
-Dfile.encoding=GBK -Ddefault.client.encoding=GBK -Duser.language=zh -Duser.region=CN |
补充:java执行cmd命令,返回结果中文乱码问题解决
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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(); } } |
我的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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(); } } } } } |
结果
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://www.cnblogs.com/liuys635/p/13355532.html