做网站需要学php吗,云推广,大气网站背景图,wordpress内容评论可见文件夹用来把文件包裹起来#xff0c;褪去这些外衣#xff0c;说到底拷贝文件夹也就是拷贝文件 模拟实例#xff1a;将F:/Picture/test 文件夹 拷贝到 F:/Picture/dir文件夹 该实例中test文件夹下只包含了test.txt文件 步骤分析#xff1a; 1.通过路径得到File对象 2.递归查…文件夹用来把文件包裹起来褪去这些外衣说到底拷贝文件夹也就是拷贝文件 模拟实例将F:/Picture/test 文件夹 拷贝到 F:/Picture/dir文件夹 该实例中test文件夹下只包含了test.txt文件 步骤分析 1.通过路径得到File对象 2.递归查找子孙级文件夹或者文件 3.复制文件同文件拷贝 那么重点是在第二个步骤我们可以通过File对象的listFiles方法得到目标文件夹下所包括的文件listFiles方法返回一个泛型为File的集合list由此我们就得到了test文件夹下所有的文件通过foreach循环语句遍历这个list得到的每一个File对象首先要做的就是判断这个File对象是文件还是文件夹如果是文件就可直接copy如果是文件夹则需要再通过listFiles方法得到目标文件夹下所包括的文件步骤与上面一致这也就是递归的思想 需要注意的一点是我们需要把整个test文件夹拷贝到dir文件夹那么当遍历到test文件夹下的test.txt文件时我们在拷贝的时候需要重新创建一个新的目标文件dir/test/text.txt.这就需要File的另一个构造方法 File(File parent, String child) 根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例 在得到dir这个文件夹的时候也应该用上述构造方法得到dir/testFile新对象 在拷贝文件的时候使用了不同的流之前拷贝文件使用的FileInputStream与FileOutputStream 这里使用了BufferedInputStream与BufferedOutputStream使用方法相似 InputStream is new BufferedInputStream(new FileInputStream(src));
OutputStream os new BufferedOutputStream(new FileOutputStream(dest)); package file; 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.io.InputStream;import java.io.OutputStream; public class FileUtil { /** * param args */ public static void main(String[] args) { // 源目录 String srcPath C:\\Users\\Administrator\\Desktop\\bbb; // 目标目录 String destPath C:\\Users\\Administrator\\Desktop\\ccc; //进行拷贝 try { copyDir(srcPath, destPath); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 通过路径获得File对象 * * param src源路径 * param dest目标路径 * throws IOException * throws FileNotFoundException */ public static void copyDir(String srcPath,String destPath) throws FileNotFoundException, IOException{ //拒绝自己拷贝给自己 if(srcPath.equals(destPath)){ return ; } File srcnew File(srcPath); File dest new File(destPath); copyDir(src,dest); } /** * 拷贝文件夹 * param src 源File对象 * param dest 目标File对象 * throws IOException * throws FileNotFoundException */ public static void copyDir(File src,File dest) throws FileNotFoundException, IOException{ if(src.isDirectory()){ //文件夹 dest new File(dest,src.getName()); if(dest.getAbsolutePath().contains(src.getAbsolutePath())){ System.out.println(父目录不能拷贝到子目录中); return; } } copyDirDetail(src,dest); } /** * 拷贝文件夹细节 * param src * param dest */ public static void copyDirDetail(File src,File dest) throws FileNotFoundException,IOException{ if(src.isFile()){ //文件 copyFile(src, dest); }else if(src.isDirectory()){ //文件夹 //确保目标文件夹存在 dest.mkdirs(); //获取下一级目录|文件 for(File sub:src.listFiles()){ copyDirDetail(sub,new File(dest,sub.getName())); } } } /** * 文件的拷贝得到File对象 * param 源文件路径 * param 目录文件路径 * throws FileNotFoundException,IOException * return */ public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException { //1、建立联系 源(存在且为文件) 目的地(文件可以不存在) copyFile(new File(srcPath),new File(destPath)); } /** * 文件的拷贝 * param 源文件File对象 * param 目录文件File对象 * throws FileNotFoundException,IOException * return */ public static void copyFile(File src,File dest) throws FileNotFoundException,IOException { if(! src.isFile()){ //不是文件或者为null System.out.println(只能拷贝文件); throw new IOException(只能拷贝文件); } //dest为已经存在的文件夹不能建立于文件夹同名的文件 if(dest.isDirectory()){ System.out.println(dest.getAbsolutePath()不能建立于文件夹同名的文件); throw new IOException(dest.getAbsolutePath()不能建立于文件夹同名的文件); } //2、选择流 InputStream is new BufferedInputStream(new FileInputStream(src)); OutputStream os new BufferedOutputStream(new FileOutputStream(dest)); //3、文件拷贝 循环读取写出 byte[] flush new byte[1024]; int len 0; //读取 while(-1!(lenis.read(flush))){ //写出 os.write(flush, 0, len); } os.flush(); //强制刷出 //关闭流 os.close(); is.close(); } }转载于:https://www.cnblogs.com/chinaifae/p/10328796.html