代码windows和linux命令,Java代码操作Windows命令与Linux命令

1. 操作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地址”, “用户名”, “密码”);

String fileName = rec.execute(“cd /usr/HDWork/in ; ls -lrt|sed -n “2, 1p”|awk ‘{print $9}’”);//查询/usr/HDWork/in 目录下的 时间最小的文件名

注*两条Linux命令同时执行 用 “ ; ”分割

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值