---
title: "JAVA复制文件的操作"
author: "Hello_Kitty_O"
date: "2010-06-09T23:46:19Z"
tags: ["C", "C#", "C++", "F#", "Java"]
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2010-06-09T23:47:35Z"
id: 688442
url: "https://hello-kitty-o.iteye.com/blog/688442"
---

### Java代码 [![复制代码](http://acdf111.iteye.com/images/icon_copy.gif)](http://acdf111.iteye.com/blog/413478 "复制代码")

01. /\**
02. * 复制整个文件夹内容
03. \*
04. * @param oldPath
05. *            String 原文件路径 如：c:/fqf
06. * @param newPath
07. *            String 复制后路径 如：f:/fqf/ff
08. * @return boolean
09. \*/
10. **private** **static** **void** copyFolder(String oldPath, String newPath) {
11. **try** {
12. (**new** File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
13. File a = **new** File(oldPath);
14. String\[] file = a.list();
15. File temp = **null**;
16. **for** (**int** i = 0; i &lt; file.length; i++) {
17. **if** (oldPath.endsWith(File.separator)) {
18. temp = **new** File(oldPath + file\[i]);
19. } **else** {
20. temp = **new** File(oldPath + File.separator + file\[i]);
21. }
22. **if** (temp.isFile()) {
23. FileInputStream input = **new** FileInputStream(temp);
24. FileOutputStream output = **new** FileOutputStream(newPath + "/" + (temp.getName()).toString());
25. **byte**\[] b = **new** **byte**\[1024 * 5];
26. **int** len;
27. **while** ((len = input.read(b)) != -1) {
28. output.write(b, 0, len);
29. }
30. output.flush();
31. output.close();
32. input.close();
33. }
34. **if** (temp.isDirectory()) {// 如果是子文件夹
35. copyFolder(oldPath + "/" + file\[i], newPath + "/" + file\[i]);
36. }
37. }
38. } **catch** (Exception e) {
39. System.out.println("复制整个文件夹内容操作出错");
40. e.printStackTrace();
41. }
42. }