WEB开发网      婵犻潧鍊婚弲顐︽偟椤栨稓闄勯柦妯侯槸閻庤霉濠婂骸浜剧紒杈ㄥ笚閹峰懘鎮╅崹顐ゆ殸婵炴垶鎸撮崑鎾趁归悩鐑橆棄闁搞劌瀛╃粋宥夘敃閿濆柊锕傛煙鐎涙ê鐏f繝濠冨灴閹啴宕熼鍡╀紘婵炲濮惧Λ鍕叏閳哄懎绀夋繛鎴濈-楠炪垽鎮归崶褍妲婚柛銊ュ缁傚秹鏁撻敓锟� ---闂佹寧娲╅幏锟�
开发学院手机开发Android 开发 Android应用中执行Linux命令 阅读

Android应用中执行Linux命令

 2013-03-19 16:43:47 来源:开发学院 闂侀潧妫撮幏锟�闂佸憡鍨电换鎰版儍椤掑倵鍋撳☉娆嶄沪缂傚稄鎷�婵犫拃鍛粶闁靛洤娲ㄩ埀顒佺⊕閵囩偟绱為敓锟�闂侀潧妫撮幏锟�  闂佺ǹ绻楀▍鏇㈠极閻愬搫绾ч柕濠忕細閼割亜顪冪€n剙浠ф繛鍫熷灥椤曘儵顢欓悡搴ば�
核心提示: 修改某一文件的权限为777为例:/** 执行Linux命令,并返回执行结果,Android应用中执行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);  

Tags:Android 应用 执行

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接