栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

单级文件夹的复制

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

单级文件夹的复制

package JB2015;

import java.io.*;


public class JDG {
    public static void main(String[] args) throws IOException {
        File  file=new File("D:\jdg");//源文件
        
        String filename= file.getName();//获得源文件夹的名字

        File file1=new File("D:\jdg1",filename);
        //目的文件夹

         if (!file1.exists()){
             file1.mkdir();//判断目的文件夹是否存在,不存在就创建一个
         }

         File [] listFiles=file.listFiles(); //获取源文件夹的全部文件
        for (File fi:listFiles
             ) {
            String finame=fi.getName(); //获取源文件夹的文件的名字

            File destname=new File(file1,finame);
            copy(file,file1);//复制文件
        }
    }

    private static void copy(File file, File file1) throws IOException {
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file1));

        byte[] bytes = new byte[1024];
        int len;
        while ((len = bufferedInputStream.read(bytes)) != -1) {
            bufferedOutputStream.write(bytes, 0, len);
        }
        bufferedInputStream.close();
        bufferedOutputStream.close();
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/850958.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号