+ -
当前位置:首页 → 问答吧 → 用io流复制文件夹(包括文件夹里的文件)--出现的问题,无法复制文件

用io流复制文件夹(包括文件夹里的文件)--出现的问题,无法复制文件

时间:2011-09-03

来源:互联网

public static void CopyDirecotry(File fromDir, File toDir) throws Exception {
// File f = new File(fromDir.getName());
toDir.mkdir();
if (fromDir.isDirectory()) {
File[] Dirname = fromDir.listFiles();

for (int i = 0; i < Dirname.length; i++) {
if (Dirname[i].isDirectory()) {
CopyDirecotry(Dirname[i], new File(toDir.getAbsoluteFile()
+ "\\" + Dirname[i].getName()));
} else {
copyFile(Dirname[i], Dirname[i].getName());

}
}
}
}


public static void copyFile(File fromFile, String toFile)
throws IOException {
FileReader from = new FileReader(fromFile);
File ff = new File(toFile);
if (!ff.exists()) {
ff.createNewFile();
}
FileWriter to = new FileWriter(ff);
char[] ch = new char[(int) fromFile.length()];
while (from.read(ch) != -1);
from.close();
to.write(ch);
to.flush();
to.close();
System.out.println("文件成功复制!!!");
}



public static void main(String[] args) throws IOException {
try {
File fromDir = new File(getFile + File.separator + "bin");
File toDir = new File(getFile + File.separator + "bin1");
CopyDirecotry(fromDir, toDir);
} catch (Exception e) {
e.printStackTrace();
}
}

作者: yangjunoakley2010   发布时间: 2011-09-03

getFile那段代码呢?为什么不贴上来?

作者: k3108001263   发布时间: 2011-09-03

热门下载

更多