- 需求:实现通过java监听usb移动介质的拔插和拷贝记录;
找了很多软件似乎都能实现这个功能,近期公司有这个需求要融到自己的系统板块中,如何能够获取移动介质的相关信息和拷入拷出记录呢?
这个记录包含从哪个文件的路径地址。
能不能有同志提供个思路或者方法的
java监听usb记录
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
17条回答 默认 最新
- 「已注销」 2023-03-15 15:43关注
获得10.00元问题酬金 参考GPT和自己的思路:要实现通过Java监听USB移动介质的拔插和拷贝记录,可以使用JNotify库。这个库可以监听文件系统的变化,并通知监听者。具体来说,可以使用JNotify监听USB移动介质的挂载和卸载事件,以及文件的创建、修改、删除等事件。然后根据这些事件的信息记录拷入拷出的记录。
以下是一个基本的示例代码,它使用JNotify库监听指定目录的文件变化:
import net.contentobjects.jnotify.JNotify; import net.contentobjects.jnotify.JNotifyListener; public class USBListener { public static void main(String[] args) throws Exception { // 监听的目录 String path = "D:\\"; // 监听的事件类型 int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED; // 是否监听子目录 boolean watchSubtree = true; // 创建监听器 JNotifyListener listener = new JNotifyListener() { public void fileRenamed(int wd, String rootPath, String oldName, String newName) { System.out.println("renamed " + rootPath + " : " + oldName + " -> " + newName); } public void fileModified(int wd, String rootPath, String name) { System.out.println("modified " + rootPath + " : " + name); } public void fileDeleted(int wd, String rootPath, String name) { System.out.println("deleted " + rootPath + " : " + name); } public void fileCreated(int wd, String rootPath, String name) { System.out.println("created " + rootPath + " : " + name); } }; // 开始监听 int watchId = JNotify.addWatch(path, mask, watchSubtree, listener); // 等待用户输入,结束监听 System.in.read(); JNotify.removeWatch(watchId); } }
这个示例代码会监听指定目录的文件创建、删除、修改和重命名事件,并将这些事件的信息打印到控制台。你可以根据这些事件的信息来记录拷入拷出的记录。
如果你需要获取移动介质的相关信息,比如移动介质的序列号、容量、文件系统等,可以使用Java的File类和FileSystem类。具体来说,可以使用File类来表示移动介质的根目录,然后使用FileSystem类来获取移动介质的相关信息。以下是一个示例代码,它可以列出所有的移动介质,并获取它们的容量和文件系统类型:
import java.io.File; import java.io.IOException; import java.nio.file.FileStore; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; public class USBWatcher { public static void main(String[] args) throws IOException { File[] roots = File.listRoots(); for (File root : roots) { if (isUSB(root)) { System.out.println("USB found: " + root.getPath()); System.out.println("Capacity: " + getCapacity(root)); System.out.println("File system type: " + getFileSystemType(root)); } } } private static boolean isUSB(File root) throws IOException { String type = Files.getFileStore(root.toPath()).type(); return type.equals("vfat") || type.equals("exfat") || type.equals("ntfs"); } private static String getFileSystemType(File root) throws IOException { return Files.getFileStore(root.toPath()).type(); } private static long getCapacity(File root) throws IOException { Path path = root.toPath(); FileStore store = Files.getFileStore(path); return store.getTotalSpace(); } }
注意:需要在程序中添加文件系统权限,否则可能无法获取移动介质的相关信息。
解决 1无用
悬赏问题
- ¥15 SPSS分类模型实训题步骤
- ¥15 求解决扩散模型代码问题
- ¥15 工创大赛太阳能电动车项目零基础要学什么
- ¥20 limma多组间分析最终p值只有一个
- ¥15 nopCommerce开发问题
- ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
- ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
- ¥15 pycharm输出和导师的一样,但是标红
- ¥15 想问问富文本拿到的html怎么转成docx的
- ¥15 我看了您的文章,遇到了个问题。