Chinaunix首页 | 论坛 | 博客
  • 博客访问: 412928
  • 博文数量: 18
  • 博客积分: 128
  • 博客等级: 入伍新兵
  • 技术积分: 152
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-02 15:05
文章分类
文章存档

2012年(10)

2011年(8)

我的朋友

分类:

2011-10-15 20:34:43

我们都知道要使用Java调用Linux的命令的都是使用这个语句RunTime.getRunTime().exec("ls -al"); 如果你不知道是这样使用的话说明你没有这方面的经验,或者你不擅长java。这样的话你一定知道C/C++中的int execl()函数,要不然,算了,我知道了你不是程序员。

废话少说,Java的RunTime.getRunTime().exec("ls -al"); 执行之后你是什么都看不到的,因为这个语句返回的是Process进程类的子类实例,因此你要想输出东西的话,那就要用到输入输出流了,我贴个代码出来:

import java.io.*;
public class Test{

public static void main(String[] args) throws Exception{
try{
Process process=Runtime.getRuntime().exec("ls ./");
InputStreamReader reader = new InputStreamReader(process.getInputStream());
LineNumberReader line = new LineNumberReader(reader);
String str;
while((str=line.readLine())!=null){
System.out.println(str);
}

}catch (Exception e){
e.printStackTrace();
}

System.out.println("done !!!");
}
阅读(3034) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~