ITeye博客

JAVA复制文件的操作

Hello_Kitty_O · 2010-06-09 23:46 · Java · 编程语言 · C · Java · C++ · C# · F# · Markdown

Java代码 复制代码

  1. /**
  2. * 复制整个文件夹内容
  3. *
  4. * @param oldPath
  5. *            String 原文件路径 如:c:/fqf
  6. * @param newPath
  7. *            String 复制后路径 如:f:/fqf/ff
  8. * @return boolean
  9. */
  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 < 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. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。