- public class CopyDirDemo {
- public static void main(String[] args) throws IOException {
- File destFile = new File("D:\\copy1");
- File newFile = new File("D:\\copy2");
- copyDir(destFile, newFile);
- }
- public static void copyDir(File destFile, File newFile) throws IOException {
- newFile.mkdir();
- File[] fileList = destFile.listFiles();
- for (File file : fileList) {
- if (file.isDirectory()) {
- copyDir(new File(destFile, file.getName()), new File(newFile, file.getName()));
- } else {
-
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(destFile));
- byte[] bys = new byte[1024];
- for (int len = 0; (len = bis.read(bys)) != -1;) {
- bos.write(bys, 0, len);
- }
- }
- }
- }
- }
复制代码 报的错是!FileNotFoundException: D:\copy2 (拒绝访问。)
API里是:当试图打开指定路径名表示的文件失败时,抛出此异常。
|
|