赞
踩
package HomeWork;
import java.io.*;
import java.util.Iterator;
public class HomeWork {
public static void main(String[] args) throws IOException {
//1、定义方法将指定文件夹中所有文件复制到指定位置。方法参数:1.文件夹路径 2.目标路径
String s1="D:\\logs";
String s2="D:\\基本资料\\HomeWork618";
copy(s1, s2);
}
public static void copy(String old,String news) throws IOException {
File fo=new File(old);//旧文件
File fn= new File(news);//新文件---目的地
if (!fo.isDirectory()) {//如果表示的是一个目录则 isDirectory 返回true
System.out.println(fo+"不是目录");
}
String[] list = fo.list();//得到旧文件的所有文件目录
if (!fn.exists()) {//exists(),判断目的地的目标文件是否存在,是否已创建
fn.createNewFile();//不存在就创建一个
}
for(int a=0;a<list.length;a++) {
FileInputStream fi1=new FileInputStream(fo+"\\"+list[a]);
FileOutputStream fo1=new FileOutputStream(fn+"\\"+list[a]);//目的地
byte[] b1=new byte[1024];
int len;
while( (len=fi1.read(b1))!=-1 ) {
fo1.write(b1, 0, len);
}
fi1.close();
fo1.flush();
fo1.close();
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。