第三题:- package com.heima.demo;
- import java.util.Scanner;
- import java.io.File;
- public class Demo {
- public static void main(String[] args) throws IOException {
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入源文件夹路径名:");
- File src = new File(sc.nextLine());
- System.out.println("请输入目的文件夹路径名:");
- File dest = new File(sc.nextLine());
- copyDir(src, dest);
- }
- public static void copyDir(File src, File dest) throws IOException {
- File newDir = new File(dest, src);
- newDir.mkdir();
- File[] files = dest.listFiles();
- for(File f : files) {
- if(f.isFile()) {
- BufferedInputStream bufis = new BufferedInputStream(new FileInputStream(f));
- BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream(new File(newDir, f.getName())));
- byte[] buf = new byte[8192];
- int len = 0;
- while(( len = bufis.read(buf))) {
- bufos.write(buf, 0 , len);
- bufos.flush();
- }
- bufos.close();
- bufis.close();
- }else {
- copyDir(f, newDir);
- }
- }
- }
复制代码 |