- import java.io.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class DirDemo {
- private static File getDir() throws IOException {
- System.out.println("请输入路径:");
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String line = br.readLine();
- File f = new File(line);
- return f;
- }
- }
- public class Test3 {
- public static void main(String[] args) throws IOException {
-
- File file = DirUtil.getDir();
- File dest = DirUtil.getDir();
- fileCopy(file,dest);
- }
- public static void fileCopy(File src, File dest) throws IOException {
- File newDest = new File(dest, src.getName());
- newDest.mkdir();
- File[] fi = src.listFiles();
- for (File f : fi) {
- if (f.isDirectory()) {
- fileCopy(f, newDest);
- } else {
- FileInputStream fs = new FileInputStream(f);
- FileOutputStream fo = new FileOutputStream(new File(newDest, f.getName()));
- byte[] b = new byte[1024];
- int len;
- while ((len = fs.read(b)) != -1) {
- fo.write(b, 0, len);
- }
- fs.close();
- fo.close();
- }
- }
- }
- }
复制代码 |