用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();
}
}
// 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
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28