JAVA代码执行shell命令 并解析
2012-01-17 15:38
761 查看
在Android可能有的系统信息没有直接提供API接口来访问,为了获取系统信息时我们就要在用shell指令来获取信息,这时我们可以在代码中来执行命令 ,这里主要用到ProcessBuilder 这个类.
代码部分 :
[java] view
plaincopy
package com.yin.system_analysis;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private final static String[] ARGS = {"ls","-l"};
private final static String TAG = "com.yin.system";
Button mButton;
TextView myTextView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button) findViewById(R.id.myButton);
myTextView = (TextView) findViewById(R.id.textView);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myTextView.setText(getResult());
}
});
}
public String getResult(){
ShellExecute cmdexe = new ShellExecute ( );
String result="";
try {
result = cmdexe.execute(ARGS, "/");
} catch (IOException e) {
Log.e(TAG, "IOException");
e.printStackTrace();
}
return result;
}
private class ShellExecute {
/*
* args[0] : shell 命令 如"ls" 或"ls -1";
* args[1] : 命令执行路径 如"/" ;
*/
public String execute ( String [] cmmand,String directory)
throws IOException {
String result = "" ;
try {
ProcessBuilder builder = new ProcessBuilder(cmmand);
if ( directory != null )
builder.directory ( new File ( directory ) ) ;
builder.redirectErrorStream (true) ;
Process process = builder.start ( ) ;
//得到命令执行后的结果
InputStream is = process.getInputStream ( ) ;
byte[] buffer = new byte[1024] ;
while ( is.read(buffer) != -1 ) {
result = result + new String (buffer) ;
}
is.close ( ) ;
} catch ( Exception e ) {
e.printStackTrace ( ) ;
}
return result ;
}
}
}
代码部分 :
[java] view
plaincopy
package com.yin.system_analysis;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private final static String[] ARGS = {"ls","-l"};
private final static String TAG = "com.yin.system";
Button mButton;
TextView myTextView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button) findViewById(R.id.myButton);
myTextView = (TextView) findViewById(R.id.textView);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myTextView.setText(getResult());
}
});
}
public String getResult(){
ShellExecute cmdexe = new ShellExecute ( );
String result="";
try {
result = cmdexe.execute(ARGS, "/");
} catch (IOException e) {
Log.e(TAG, "IOException");
e.printStackTrace();
}
return result;
}
private class ShellExecute {
/*
* args[0] : shell 命令 如"ls" 或"ls -1";
* args[1] : 命令执行路径 如"/" ;
*/
public String execute ( String [] cmmand,String directory)
throws IOException {
String result = "" ;
try {
ProcessBuilder builder = new ProcessBuilder(cmmand);
if ( directory != null )
builder.directory ( new File ( directory ) ) ;
builder.redirectErrorStream (true) ;
Process process = builder.start ( ) ;
//得到命令执行后的结果
InputStream is = process.getInputStream ( ) ;
byte[] buffer = new byte[1024] ;
while ( is.read(buffer) != -1 ) {
result = result + new String (buffer) ;
}
is.close ( ) ;
} catch ( Exception e ) {
e.printStackTrace ( ) ;
}
return result ;
}
}
}
相关文章推荐
- Android中执行java命令的方法及java代码执行并解析shell命令
- 通过Java代码执行shell命令/脚本
- java代码中执行shell或调用系统命令
- 使用Python生成Shell命令,批量执行程序代码解析
- Android中级篇之用JAVA代码执行shell命令
- Android java代码中如何执行shell命令
- 使用Java代码执行系统命令/shell命令, 并获取输出结果
- Android中级篇之用JAVA代码执行shell命令
- 在Linux操作系统上执行java代码,执行交互式命令
- java 中调用window系统中的文件,或者执行命令(shell、.CMD、.EXE)并获取返回值
- Android Java执行Shell命令
- java通过代码登录远程linux服务器并执行linux命令
- windows 下 java程序jsch连接远程linux服务器执行shell命令
- 纯JAVA实现远程执行SSH2主机的SHELL命令
- java 执行shell命令
- 重复调用ab命令的shell代码和整理ab结果的java代码
- android执行shell命令,top 命令解析
- java执行windows、shell命令
- JAVA 执行 dos/shell命令的实现
- 通过java代码实现dos环境下多条命令的执行