package cn.hmm.Copy;
import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class newCopy {
public static void main(String[] args) throws IOException {
// TODO 自动生成的方法存根
File f1 = new File("C:/abc");
long startime = 0;
System.out.println(startime = System.currentTimeMillis());
find(f1);
System.out.println(System.currentTimeMillis()-startime+"毫秒");
/*File f2 = new File("d:\\2.txt");
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
copy(in,out);
in.close();
out.close();*/
}
private synchronized static void find(final File f1){
File f2 = null;
if(f1.isDirectory()){
String Path = f1.getAbsolutePath();
System.out.println(Path);
String regex = "^[a-z]|[A-Z]{1}:{1}";
String[] Path1 = Path.split(regex);
Path = "D:"+Path1[1];
f2 = new File(Path);
System.out.println(f2.mkdirs());
System.out.println(Path);
File[] fs = f1.listFiles();
for(File f:fs){
find(f);
}
}
else if(f1.isFile()){
// TODO 自动生成的方法存根
System.out.println(Thread.currentThread().toString());
copyfile(f1);
}
}
private static void copyfile(File f1) {
File f2;
String Path = f1.getAbsolutePath();
System.out.println(Path);
String regex = "^[a-z]|[A-Z]{1}:{1}";
String[] Path1 = Path.split(regex);
Path = "D:"+Path1[1];
f2 = new File(Path);
InputStream in;
try {
in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
copy(in,out);
in.close();
out.close();
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
private static void copy(final InputStream ips,final OutputStream out) {
int len = 0;
byte[] bytes = new byte[1024];
try {
while((len=ips.read(bytes))!=-1){
out.write(bytes);
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
复制目录和目录下的所有文件
最新推荐文章于 2022-10-24 15:24:21 发布