java实现系统多级文件夹复制
时间:2022-12-16 09:31:32|栏目:JAVA代码|点击: 次
本文实例为大家分享了java实现系统多级文件夹复制的具体代码,供大家参考,具体内容如下
package com.jae; import java.io.*; //复制文件夹内的内容,包含多级文件夹 public class Test2 { public static void main(String[] args) throws Exception { //原文件夹地址 File resPath = new File("E:\\Java\\分享"); File destPath = new File("E:\\"); //method(resPath, destPath); copy(resPath, destPath); } public static void copy(File src,File dest) throws Exception { File newDir = new File(dest,src.getName()); newDir.mkdir(); File[] subFiles = src.listFiles(); for (File subFile : subFiles) { if(subFile.isFile()){ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile)); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream( new File(newDir,subFile.getName()))); int b; byte[] bytes = new byte[1024 * 8]; while((b = bis.read(bytes)) != -1){ bos.write(bytes,0,b); } bis.close(); bos.close(); }else{ copy(subFile,newDir);//递归调用 } } } public static void method(File dir, File destPath) throws IOException { //获取源路径的文件 File[] files = dir.listFiles(); //根目录文件夹名 String name1 = dir.getName(); //遍历源路径的文件 for (File file : files) { if (file.isFile()) { StringBuilder sb = new StringBuilder(destPath.getAbsolutePath()); sb.append("\\").append(file.getName()); //获取盘符后面的路径和文件名 //String s = absolutePath.split(":")[1]; //创建输入流,封装获取到的文件的绝对路径 FileInputStream fis = new FileInputStream(file.getAbsolutePath()); //目标路径,定义目标路径的盘符,和要复制的文件路径和文件名 //FileOutputStream fos = new FileOutputStream("F:" + s); FileOutputStream fos = new FileOutputStream(sb.toString()); //复制文件操作 int len; byte[] bytes = new byte[1024 * 8]; while ((len = fis.read(bytes)) != -1) { fos.write(bytes, 0, len); fos.flush(); } fos.close(); fis.close(); } else { StringBuilder sb = new StringBuilder(destPath.getAbsolutePath()); sb.append("\\").append(name1).append("\\").append(file.getName()); /*String name1 = file.getName(); //获取文件夹的路径 String name = file.getPath(); //获取盘符:后的文件夹路径 String s = name.split(":")[1];*/ //创建文件夹路径 //File file1 = new File("F:" + name1); File file1 = new File(sb.toString()); file1.mkdirs(); //System.out.println(name); method(file, file1); } } } }
再为大家补充一段代码:Java递归复制多级目录及文件,感谢原作者的分享
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* * 需求:复制多级文件夹 * * 数据源:D:\\1 * 目的地:F:\\新建文件夹 * * 分析: * A:封装数据源File * B:封装目的地File * C:判断该File是文件夹还是文件 * a:是文件夹 * 就在目的地目录下创建该文件夹 * 获取该File对象下的所有文件或者文件夹File对象 * 遍历得到每一个File对象 * 回到C * b:是文件 * 就复制(字节流) */ public class C1 { public static void main(String[] args) throws IOException { File srcFolder = new File("D:\\1"); File dstFolder = new File("F:\\新建文件夹"); judge(srcFolder,dstFolder); } private static void judge(File srcFolder,File dstFolder) throws IOException { if(srcFolder.isDirectory()){ File newFolder = new File(dstFolder,srcFolder.getName()); newFolder.mkdir(); File[] fileArr = srcFolder.listFiles(); for(File f:fileArr){ judge(f, newFolder); } }else{ File newFile = new File(dstFolder,srcFolder.getName()); // System.out.println(newFile); copyFile(srcFolder,newFile); } } private static void copyFile(File srcFolder, File newFile) throws IOException { // TODO Auto-generated method stub BufferedInputStream bis = new BufferedInputStream(new FileInputStream( srcFolder)); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(newFile)); byte[] bys = new byte[1024]; int len = 0; while ((len = bis.read(bys)) != -1) { bos.write(bys, 0, len); } bos.close(); bis.close(); } }
栏 目:JAVA代码
下一篇:关于Springboot2.x集成lettuce连接redis集群报超时异常Command timed out after 6 second(s)
本文标题:java实现系统多级文件夹复制
本文地址:http://www.codeinn.net/misctech/221498.html