java文件夹及其子目录子文件的复制

    xiaoxiao2026-09-21  4

    package test815.night;

    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; import java.util.Arrays; import java.util.Scanner;

    public class FileTest { private static boolean flag = false;

    public static void main(String[] args) { // TODO Auto-generated method stub String path = null;// "D:\\t";//要复制的文件夹 String path2 = null;// "D:\\tt"; String info = "请输入您要复制的文件的路径: 如【D:\\t 如果不输入盘符 ,则默认当前目录】"; path = inputPath(info); String info2 = "请输入您要粘贴的文件的路径: 如【D:\\tt 如果不输入盘符 ,则默认当前目录】"; path2 = inputPath(info2); System.out.println("开始复制,请稍等……"); isFile(path, path2); if (flag) { System.out.println("复制完毕end"); } else { System.out.println("复制失败end"); } } public static String inputPath(String info) { System.out.println(info); Scanner sc = new Scanner(System.in); String pathString = sc.nextLine(); return pathString; } // 对原路径进行判断 public static void isFile(String path, String path2) { File file = new File(path);// 获取path地址对应的文件 if (file.exists()) { File newpathFile = new File(path2);// 建立新的路径文件 newpathFile.mkdir(); String[] str = file.list();// 获得该文件下的子文件和子目录的名称 // System.out.println(str.length); System.out.println(path + " 中原有文件为: " + Arrays.toString(str)); for (String string : str) {// 遍历 String oldpath = path + "\\" + string;// 组合新的路径 String newpath = path2 + "\\" + string;// 对应的新文件夹 组合路径 File oldFile = new File(oldpath);// 取得对应的文件 if (oldFile.isDirectory()) {// 判断是否为目录 File newFile = new File(newpath);// 在对应的新路径下建立文件夹 newFile.mkdir(); isFile(oldpath, newpath);// 递归 进入到子文件夹内 } else { copy(oldpath, newpath);// 如果是文档,则开始复制 } } } else { System.out.println(path + " 原始路径有误!"); } } public static void copy(String path, String path2) { flag = false; FileInputStream fis = null; FileOutputStream fos = null; byte[] bs = new byte[2048]; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { fis = new FileInputStream(path); fos = new FileOutputStream(path2); bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); int x = bis.read(bs); while (x != -1) { bos.write(bs, 0, x); x = bis.read(bs); } bos.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (fis != null) { try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (bos != null) { try { bos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } flag = true; } }

    }

    转载请注明原文地址: https://ju.6miu.com/read-1312167.html
    最新回复(0)