java执行cmd命令并获取输出结果

1.java执行cmd命令并获取输出结果

 import java.io.BufferedReader;
import java.io.InputStreamReader; import org.apache.commons.lang3.text.StrBuilder; /**
*
* @author user1
*/
public class DeleteProgram {
public static void run() {
Runtime runtime = Runtime.getRuntime();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(runtime.exec("ipconfig").getInputStream()));
//StringBuffer b = new StringBuffer();
String line=null;
StringBuffer b=new StringBuffer();
while ((line=br.readLine())!=null) {
b.append(line+"\n");
}
System.out.println(b.toString());
} catch (Exception e) {
e.printStackTrace();
} } public static void main(String[] args) {
DeleteProgram delp = new DeleteProgram();
delp.run();
} }

2.获取您需要的字段

public static String getbackage(String path){
Process p;
//test.bat中的命令是ipconfig/all
String cmd = "aapt dump badging " + path;
// String cmd="jarsigner -verify -verbose -certs C:\\Users\\Administrator\\Desktop\\PandaClient.apk";
String resultstr = null;
try
{
//执行命令
p = Runtime.getRuntime().exec(cmd);
//取得命令结果的输出流
InputStream fis=p.getInputStream();
//用一个读输出流类去读
//用缓冲器读行
BufferedReader br=new BufferedReader( new InputStreamReader(fis,"GB2312"));
String line=null;
//直到读完为止
int i = 0;
while((line=br.readLine())!=null)
{
if(line.contains("package:")){ //解析符合自己需要的內容,获取之后,直接返回。 resultstr = line;
break;
}
// resultstr=line;
i++;
}
}
catch (IOException e)
{
e.printStackTrace();
}
return resultstr;
}
     public static void main(String[] args) {
String url="www.taobao.com";
String response=null;
response=getSingle(url); System.out.println(response);
}
上一篇:java中执行cmd命令


下一篇:Effective C++笔记(五):实现