-
操作Windows:
/* *1.txt 从work目录移动到in目录 * */ public static void mvIn(){ String[] command = { "cmd", }; Process p = null; try { p = Runtime.getRuntime().exec(command); new Thread(new SyncPipe(p.getErrorStream(), System.err)).start(); new Thread(new SyncPipe(p.getInputStream(), System.out)).start(); PrintWriter stdin2 = new PrintWriter(p.getOutputStream()); stdin2.println("move E:\\work\\"+fileName+"\tE:\\in");//将work下的文件时间最小的文件转移到 stdin2.close();//执行命令 }catch (Exception e) { System.out.println(e.getMessage()+"有异常"); } }
//新建一个接口 (上下为两个类)
class SyncPipe implements Runnable{
public SyncPipe(InputStream istrm, OutputStream ostrm) {
istrm_ = istrm;
ostrm_ = ostrm;
}
public void run() {
try{
final byte[] buffer = new byte[1024];
for (int length = 0; (length = istrm_.read(buffer)) != -1;){
ostrm_.write(buffer, 0, length);
}
}
catch (Exception e) {
throw new RuntimeException("处理命令出现错误:"+e.getMessage());
}
}
private final OutputStream ostrm_;
private final InputStream istrm_;
}
2.操作Linux:
RemoteExecuteCommand rec = new RemoteExecuteCommand("IP地址", "用户名", "密码");
//查询/usr/HDWork/in 目录下的 时间最小的文件名
String fileName = rec.execute("cd /usr/HDWork/in ; ls -lrt|sed -n \"2, 1p\"|awk '{print $9}'");
注*两条Linux命令同时执行 用 “ ; ”分割