在Java或者android开发中,我们有时候需要使用Java代码来执行Linux命令,做一些删除或者读取文件的操作,主要用到了Java Process类和Runtime类里面的一些方法来删除或者读取Linux设备上的文件,代码如下所示:
try{
Process pp = Runtime.getRuntime().exec("ls -a /sdcard/music");
InputStreamReader is = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(is);
String line = "";
do {
line = input.readLine();
if (line == null) {
break;
}
//输出每一行数据
System.out.println(line);
}while (true);
}catch (Exception ex){
ex.printStackTrace();
}代码非常简单,复制过去即可使用了!