java的执行linux命令 [英] java executing linux command

查看:232
本文介绍了java的执行linux命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的试图从Java code执行的Linux commant'猫',但它不工作。

I`m trying to execute linux commant 'cat' from java code, but it does not working.

Runtime.getRuntime().exec("cat /home/roman/logs/*");  

和它的工作以及对单个文件的猫

And it working well for cat of single file

Runtime.getRuntime().exec("cat /home/roman/logs/mylog.log");

我的问题是如何对猫从Java的一些目录?

My question is how to cat all files on some dir from java ?

推荐答案

您可以把所有的文件目录下,成为一个收集和遍历它:

You could put all files under the dir into a collection and iterate over it:

File[] files = dir.listFiles();
for (File f : files) {
  Runtime.getRuntime().exec("cat "+dir.getAbsolutePath()+File.separator+f.getName());
}

这篇关于java的执行linux命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆