- package in.itcast_Test;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class FileTest {
- /**
- * 复制文件夹 遍历文件 1.先进行判断是否是文件夹 如果是文件夹的话 进行创建文件夹 2.如果不是文件夹的话 直接复制. 3.在文件夹下进行遍历..
- *
- *
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- File file = new File("F:\\笔记");// 创建源文件的file对象
- File file1 = new File("f:\\复制文件夹1"); // 目标文件夹
- file1.mkdir(); // 先创建目标文件夹
- xx(file, file1);
- }
- public static void xx(File file, File file1) throws FileNotFoundException,
- IOException {
- File[] li = file.listFiles();
- for (File file2 : li) {
- if (file2.isDirectory()) {
- File f = new File(file1, file2.getName());
- f.mkdir();
- xx(file2, f);
- } else {
- File f2 = new File(file1, file2.getName());
- BufferedInputStream input = new BufferedInputStream(new FileInputStream(file2));
- BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(f2));
- byte[] by = new byte[1024];
- int len = -1;
- while ((len = input.read(by)) != -1) {
- output.write(by, 0, len);
- }
- input.close();
- output.close();
- }
- }
- }
- }
复制代码 |