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 ;
}
}
private boolean copyAssetFile(String assetsFileName,String filePath){
try {
InputStream is = getResources().getAssets().open(assetsFileName);
File outFile = new File(filePath, assetsFileName);
if(outFile.exists())
outFile.delete();
OutputStream out = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) > 0)
{
out.write(buf, 0, len);
}
is.close();
out.close();
} catch (IOException e) {
System.out.println("copy file error");
e.printStackTrace();
}
return true;
}
android执行shell命令和拷贝文件夹
最新推荐文章于 2023-11-01 09:20:24 发布