展会信息港展会大全

在Android应用中执行Linux命令 android开发
来源:互联网   发布日期:2016-01-19 12:19:29   浏览:2609次  

导读:修改某一文件的权限为777为例: ** 执行Linux命令,并返回执行结果。 * public static String exec(String[] args) { String result = ; ...

修改某一文件的权限为777为例:

/** 执行Linux命令,并返回执行结果。 */

public static String exec(String[] args) {

String result = "";

ProcessBuilder processBuilder = new ProcessBuilder(args);

Process process = null;

InputStream errIs = null;

InputStream inIs = null;

try {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

int read = -1;

process = processBuilder.start();

errIs = process.getErrorStream();

while ((read = errIs.read()) != -1) {

baos.write(read);

}

baos.write('n');

inIs = process.getInputStream();

while ((read = inIs.read()) != -1) {

baos.write(read);

}

byte[] data = baos.toByteArray();

result = new String(data);

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (errIs != null) {

errIs.close();

}

if (inIs != null) {

inIs.close();

}

} catch (IOException e) {

e.printStackTrace();

}

if (process != null) {

process.destroy();

}

}

return result;

}

下面在我们需要的地方调用上面函数即可:

String[] args = { "chmod", "777", "(文件路径)"};

exec(args);

赞助本站

人工智能实验室
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港