USB接口监听 现在做项目要做这样一个功能当有U盘插到电脑上时,自己检查U盘里面是还有goods这个文件夹,如果有就自己把goods文件夹里面的内容拷入电脑C盘下的goods目录下,这个好像要用到事件,这个功能要怎么做,最好有代码哦,分一定会给 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个用java也是要调用naive code,还不如直接用C/C++去实现。 import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.text.SimpleDateFormat;import java.util.Date;public class USB extends Thread{ /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { USB u=new USB(); u.start(); // System.out.println(bl); } /**把文件拷到指定目录下 * @param copy 要拷贝的文件 * @param paste 要粘帖的目录 * @return boolean 拷贝是否成功 * @throws IOException */ public boolean tranFile(File copy,File paste) throws IOException{// System.out.println(paste.isFile()+"123");// System.out.println(copy.isFile()+"456"); if(!copy.isFile()||paste.isFile()) return false; if(!paste.exists()) paste.mkdirs(); try{ String separator=""; separator=System.getProperties().getProperty("file.separator"); System.out.println(paste.getPath()); System.out.println(copy.getName()); System.out.println(separator); InputStream in=new FileInputStream(copy); OutputStream out=new FileOutputStream(paste.getPath()+separator+copy.getName()); byte b[]=new byte[1024]; while(in.read(b,0,1024)!=-1) { out.write(b); } in.close(); out.close(); } catch(Exception e) { System.out.println("你输入paste目录有问题!"); return false; } return true; } /** * 把文件夹拷到指定的文件夹下 * @param copy 要拷贝的文件夹 * @param paste 要粘帖的文件夹 * @return 是否拷贝成功*/ public boolean tran(File copy,File paste) throws IOException { if(copy.isFile()||paste.isFile()) return false; try{ if(!paste.exists()) paste.mkdirs(); File filelist[]=copy.listFiles(); if(filelist.length==0) return false; else { for(int i=0;i<filelist.length;i++) { if(filelist[i].isFile()) tranFile(filelist[i],paste); else { System.out.println(paste.getPath()+File.separator+filelist[i].getName()+"sdfklsdkjf"); tran(filelist[i],new File(paste.getPath()+File.separator+filelist[i].getName())); } } } } catch(Exception e){ System.out.println("你输入的两个路径有问题!"); return false; } return true; } public void run(){ char c[]={'C','D','E','F','G','H','I','J','K','L','M','N'}; int sign[]={0,0,0,0,0,0,0,0,0,0,0,0}; for(int i=0;i<c.length;i++) { File f=new File(c[i]+":\\goods\\"); if(f.exists()) { sign[i]=1; } else { sign[i]=0; } } while(true) { for(int i=0;i<c.length;i++) { try { sleep(500); System.out.println("优盘没有插入,等待中"); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } File f=new File(c[i]+":\\goods\\");// System.out.println(f.getAbsolutePath()); if(f.exists()&&sign[i]==0) { File f_des=new File("C:\\goods\\"); try { tran(f,f_des); } catch (IOException e) { e.printStackTrace(); } sign[i]=1; System.out.println("拷贝成功!"); } if(!f.exists()&&sign[i]==1) sign[i]=0; } } }} 关于java导出包的问题?! 一个线程的选择题 为什么Teacher能打印, dexing 就打印不了 希望大家帮帮我 replace和replaceAll方法的区别 请教一个字符编码的问题 JAVA 大字段处理,发生内存溢出,请问大家有什么好的建议。 这个作者有人清楚吗?rod。johnson, 在表格中,如何才能在事件产生后,动态的增加一行?? 怎么加入启动封面? 请各位高手再看此帖!还是关于用JAVA导入数据库到MYSQL的!感激不禁!必给高分相赠! 请教一个关于多线程的问题啊 正则表达式匹配
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;public class USB extends Thread{
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
USB u=new USB();
u.start();
// System.out.println(bl);
}
/**把文件拷到指定目录下
* @param copy 要拷贝的文件
* @param paste 要粘帖的目录
* @return boolean 拷贝是否成功
* @throws IOException
*/
public boolean tranFile(File copy,File paste) throws IOException{
// System.out.println(paste.isFile()+"123");
// System.out.println(copy.isFile()+"456");
if(!copy.isFile()||paste.isFile())
return false;
if(!paste.exists())
paste.mkdirs();
try{
String separator="";
separator=System.getProperties().getProperty("file.separator");
System.out.println(paste.getPath());
System.out.println(copy.getName());
System.out.println(separator);
InputStream in=new FileInputStream(copy);
OutputStream out=new FileOutputStream(paste.getPath()+separator+copy.getName());
byte b[]=new byte[1024];
while(in.read(b,0,1024)!=-1)
{
out.write(b);
}
in.close();
out.close();
}
catch(Exception e)
{
System.out.println("你输入paste目录有问题!");
return false;
}
return true;
}
/**
* 把文件夹拷到指定的文件夹下
* @param copy 要拷贝的文件夹
* @param paste 要粘帖的文件夹
* @return 是否拷贝成功*/
public boolean tran(File copy,File paste) throws IOException
{
if(copy.isFile()||paste.isFile())
return false;
try{
if(!paste.exists())
paste.mkdirs();
File filelist[]=copy.listFiles();
if(filelist.length==0)
return false;
else
{
for(int i=0;i<filelist.length;i++)
{
if(filelist[i].isFile())
tranFile(filelist[i],paste);
else
{
System.out.println(paste.getPath()+File.separator+filelist[i].getName()+"sdfklsdkjf");
tran(filelist[i],new File(paste.getPath()+File.separator+filelist[i].getName()));
}
}
}
}
catch(Exception e){
System.out.println("你输入的两个路径有问题!");
return false;
}
return true;
}
public void run(){
char c[]={'C','D','E','F','G','H','I','J','K','L','M','N'};
int sign[]={0,0,0,0,0,0,0,0,0,0,0,0};
for(int i=0;i<c.length;i++)
{
File f=new File(c[i]+":\\goods\\");
if(f.exists())
{
sign[i]=1;
}
else
{
sign[i]=0;
}
}
while(true)
{
for(int i=0;i<c.length;i++)
{ try {
sleep(500);
System.out.println("优盘没有插入,等待中");
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
File f=new File(c[i]+":\\goods\\");
// System.out.println(f.getAbsolutePath());
if(f.exists()&&sign[i]==0)
{
File f_des=new File("C:\\goods\\");
try {
tran(f,f_des);
} catch (IOException e) {
e.printStackTrace();
}
sign[i]=1;
System.out.println("拷贝成功!");
}
if(!f.exists()&&sign[i]==1)
sign[i]=0;
}
}
}}