本帖最后由 §傻、才乖 于 2014-1-19 20:41 编辑
自己写了个拷贝多级文件的代码如下:
- package javabase;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- public class CopyLayersFile {
- public static void main(String[] args) throws Exception {
- copyDir("F:/learntest", "D:");
- }
- public static void copyDir(String src, String dest) throws Exception {
- File srcFile = new File(src);
- if (!srcFile.exists())
- return;
- if (srcFile.isHidden())
- return;
- String srcPath = srcFile.getAbsolutePath();
- System.out.println(srcPath);
- String destPath = dest+"\\"+srcFile.getName();
- System.out.println(destPath);
- File destFile = new File(destPath);/**/
- File[] files = srcFile.listFiles();
- destFile.mkdir();
- for (File file : files) {
- if (file.isDirectory()) {
- copyDir(file.getAbsolutePath(), destFile.getAbsolutePath());
- } else {
- copy(file.getAbsolutePath(), destFile.getAbsolutePath());
- }
- }
- }
- private static void copy(String from, String to) throws Exception {
- InputStream in = new FileInputStream(from);
- FileOutputStream out = new FileOutputStream(to);
- byte[] bytes = new byte[1024];
- int len = 0;
- while ((len = in.read(bytes)) != -1) {
- out.write(bytes, 0, len);
- }
- in.close();
- out.close();
- }
- }
复制代码
提示错误:
F:\learntest
D:\learntest
F:\learntest\.metadata
D:\learntest\.metadata
Exception in thread "main" java.io.FileNotFoundException: D:\learntest\.metadata (拒绝访问。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:194)
at java.io.FileOutputStream.<init>(FileOutputStream.java:84)
at javabase.CopyLayersFile.copy(CopyLayersFile.java:38)
at javabase.CopyLayersFile.copyDir(CopyLayersFile.java:31)
at javabase.CopyLayersFile.copyDir(CopyLayersFile.java:29)
at javabase.CopyLayersFile.main(CopyLayersFile.java:10)
|
|