java中shell_Java 实现shell命令

packageLinuxCommand;importsun.awt.image.BufferedImageDevice;import java.io.*;importjava.nio.Buffer;importjava.util.Scanner;import staticcom.sun.deploy.cache.Cache.copyFile;public classMkdir {staticString fileName;staticString command;staticString initPath;staticString aimPath;//mkdir /home/dawn/JavaLinuxCommand/a/b/c//cp /home/dawn/JavaLinuxCommand/src/LinuxCommand /home/dawn

public static void main(String[] args) throwsIOException {

Scanner in= newScanner(System.in);

command=in.nextLine();if (command.startsWith("mkdir")) {

fileName= command.substring(5).replace(" ", "");

File f= newFile(fileName);if (!f.exists()) {

f.mkdirs();

}

}else if (command.startsWith("cp")) {

command= command.substring(2);int start = command.indexOf(" ");

command= command.substring(start + 1);

start= command.indexOf(" ");

initPath= command.substring(0, start);

aimPath= command.substring(initPath.length() + 1);

File scrFile= newFile(initPath);

File destFile= newFile(aimPath);

copyFoder(scrFile, destFile);

}else if (command.startsWith("rm")) {

command= command.substring(2);int start = command.indexOf(" ");

command= command.substring(start + 1);

start= command.indexOf(" ");

initPath= command.substring(0, start);

File filePath= newFile(initPath);

deleteFile(filePath);

}

}private static voiddeleteFile(File filePath) {if(filePath.isDirectory()) {

deleteFile(filePath);

}

filePath.delete();

}private static void copyFoder(File srcFile, File destFile) throwsIOException {if(srcFile.isDirectory()) {

File destFoder= newFile(destFile, srcFile.getName());if (!destFoder.exists()) {

destFoder.mkdir();

}

File[] listFiles=srcFile.listFiles();for(File files : listFiles) {

copyFoder(files, destFoder);

}

}else{

File aimFile= newFile(destFile, srcFile.getName());

copyFile(srcFile, aimFile);

}

}public static void copyFile(File srcFile, File aimFile) throwsIOException {

BufferedInputStream bis= new BufferedInputStream(newFileInputStream(srcFile));

BufferedOutputStream bos= new BufferedOutputStream(newFileOutputStream(aimFile));intlen;byte[] bys = new byte[1024];while ((len = bis.read(bys)) != -1) {

bos.write(bys,0, len);

}

bis.close();

bos.close();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值