import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
public class ping1 {
/**
* @param args
*/
public static String feedback = "";
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add the ubiquitous "Hello World" label.
JTextArea ta = new JTextArea(feedback);
frame.getContentPane().add(ta);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Test1");
Runtime rTime = Runtime.getRuntime();
Process p;
try{
p = rTime.exec("ping 10.11.11.67");
InputStream stdout = p.getInputStream();
InputStream stderr = p.getErrorStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout,"UTF-8"));
StringBuffer data = new StringBuffer();
String line = "";
char sch = 13; // check for ^M character
char dch = '\n'; // replace ^m by next line character
while (line != null) {
line = reader.readLine();
if (line != null){
line = new String(line.getBytes(),"UTF-8");
//System.out.println("line after :"+line);
data.append(line.replace(sch, dch)); // 7.1.00 AMTRAK QM001531720
}
data.append(System.getProperty("line.separator"));
}
// notify listeners if feedback is not suppressed
ping1.feedback = data.toString();
System.out.println("Data in feebback:"+ping1.feedback.toString());
// System.out.println("Output stream:"+strout );
// System.out.println("Error stream:"+strerr );
}
catch(IOException npe){
System.out.println(npe.getMessage());
return;
}
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
我需要输出应该与此框中提供的ping命令完全相同而不是它给出了输出? 例如命令窗口上的ping输出以 Pingwirdusgeführtfür10.11.11.67mit 32 Bytes Daten:
但是当我从代码中运行时我得到了
Ping wird ausgef?hrt f?r 10.11.11.67 mit 32 Bytes Daten:
检查für和f?r
答案 0 :(得分:0)
可能是导致此问题的jvm编码。尝试在环境变量中设置(计算机 - >属性 - >高级系统设置 - >新变量)
变量名称:JAVA_TOOL_OPTIONS
变量值:-Dfile.encoding = UTF8