U盘发现器

来源:互联网 发布:常州老年大学网络报名 编辑:程序博客网 时间:2024/06/09 17:10
package com.lx.io;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;public class USBreadFile {/**     * 更多资料欢迎浏览凯哥学堂官网:http://kaige123.com     * @author 小沫    */public static void USB() {File[] files = File.listRoots();//列出当前所有盘符//准备好集合,把所有盘符数据添加进去ArrayList<File> f1 = new ArrayList<>();ArrayList<File> f2 = new ArrayList<>();long sum;// 盘符总大小long surplus;// 盘符剩余大小long use;// 盘符已使用大小while (true) {File[] newfiles = File.listRoots();//获得到新盘符if (newfiles.length > files.length) {//如果新的盘符大于原来盘符//用Arrays工具把数组转换成集合在添加进集合f1.addAll(Arrays.asList(files));f2.addAll(Arrays.asList(newfiles));f2.removeAll(f1);//在f2和f1里面找到相同的数据进行删除掉for (File file : f2) {try {char[] c = file.getPath().toCharArray();//把路径拆分成字符System.out.print("插入:" + c[0] + " 盘");sum = file.getTotalSpace() / 1024 / 1024 / 1024;surplus = file.getUsableSpace() / 1024 / 1024 / 1024;use = sum - surplus;//列出每个盘符的信息System.out.println(c[0] + "盘总大小:" + sum + "G" +" 剩余:" + surplus + "G" + " 已用:" + use + "G");//用Runtime方法打开插入进去的U盘Runtime.getRuntime().exec("cmd /c start " + file.getPath());} catch (IOException e) {e.printStackTrace();}}files = newfiles;//更新盘符} else if (newfiles.length < files.length) {//如果新的盘符小于了原来的盘符//说明U盘已经拔出System.out.println("U盘已拔出");files = newfiles;}}}public static void main(String[] args) {USB();}}
原创粉丝点击