第一个class文件
package review;
public class Main {
public static void main(String[] args) {
new CopyFiles().copy("d://jlx", "e:");
System.out.println("复制完成!");
}
}
第二个
package review;
import java.io.File;
public class CopyFiles {
public void copy(String URLFile,String newFile){
File f1=new File(URLFile);
if(f1.isDirectory())
{
newFile=newFile+f1.getName();
new File(newFile).mkdir();
File[] URL=f1.listFiles();
for(File name:URL)
{
File f2=new File(name.toString());
if(f2.isDirectory())
{
copy(f2.getPath(),newFile+"//");
}
else{
new CopyFile().copy(f2.getPath(), newFile+"//"+f2.getName());
}
}
}
else{
new CopyFile().copy(f1.getPath(), newFile+f1.getName());
}
}
}
第三个
package review;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
public void copy(String copyURL, String newURL) {
FileInputStream read = null;
FileOutputStream write=null;
byte[] by=new byte[1024];
int x=0;
try {
read = new FileInputStream(copyURL);
} catch (FileNotFoundException e) {
System.out.println("读取文件失败");
}
try {
write = new FileOutputStream(newURL);
} catch (FileNotFoundException e) {
System.out.println("复制文件失败");
}
try {
while((x=read.read(by))!=-1)
{
write.write(by, 0, x);
}
read.close();
write.close();
} catch (IOException e) {
System.out.println("复制内容失败");
}
}
}
|
|