怎么把目录下的所有文件复制到另外目录下java
1个回答
展开全部
java将一个文件夹下的所有文件复制到另一个文件夹下
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class selfcopy {
public void IOCopy(String path,String path1){
File file = new File(path);
File file1 = new File(path1);
if(!file.exists()){
System.out.println(file.getName()+"文件不存在");
}else{
System.out.println("存在");
}
byte[] b = new byte[(int) file.length()];
if(file.isFile()){
try {
FileInputStream is= new FileInputStream(file);
FileOutputStream ps= new FileOutputStream(file1);
is.read(b);
ps.write(b);
} catch (Exception e) {
e.printStackTrace();
}
}else if(file.isDirectory()){
if(!file.exists())
file.mkdir();
String[] list = file.list();
for(int i=0;i<list.length;i++){
this.IOCopy(path+"/"+list[i], path1+"/"+list[i]);
}
}
}
public static void main(String args[]){
new selfcopy().IOCopy("D:\\device","D:\\TDDOWNLOAD");
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class selfcopy {
public void IOCopy(String path,String path1){
File file = new File(path);
File file1 = new File(path1);
if(!file.exists()){
System.out.println(file.getName()+"文件不存在");
}else{
System.out.println("存在");
}
byte[] b = new byte[(int) file.length()];
if(file.isFile()){
try {
FileInputStream is= new FileInputStream(file);
FileOutputStream ps= new FileOutputStream(file1);
is.read(b);
ps.write(b);
} catch (Exception e) {
e.printStackTrace();
}
}else if(file.isDirectory()){
if(!file.exists())
file.mkdir();
String[] list = file.list();
for(int i=0;i<list.length;i++){
this.IOCopy(path+"/"+list[i], path1+"/"+list[i]);
}
}
}
public static void main(String args[]){
new selfcopy().IOCopy("D:\\device","D:\\TDDOWNLOAD");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询