首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

Java调用Linux下令-实例

2012-08-24 
Java调用Linux命令-实例public class LinuxCommand{/*** @param args* @throws IOException*/public stati

Java调用Linux命令-实例

public class LinuxCommand{    /**     * @param args     * @throws IOException     */    public static void main(String[] args) throws IOException    {        //命令行;        String[] cmd = {"chmod","-R","775",args[0]};        //调用getRuntime().exec产生一个本地进程,并返回一个Process子类实例;        Process process = Runtime.getRuntime().exec(cmd);        //输出命令结果;        System.out.println(loadSteam(process.getInputStream()));        //输出错误信息;        System.err.println(loadSteam(process.getErrorStream()));    }        //读取流;    private static String loadSteam(InputStream inputStream) throws IOException    {        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);        StringBuffer sb = new StringBuffer();        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);        String line = null;        while ((line = bufferedReader.readLine()) != null)        {            sb.append(line);        }        return sb.toString();    }}

?

?

?

热点排行