A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© zhoujiegun 中级黑马   /  2016-5-4 23:09  /  578 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.io.*;
class Test9
{
        public static void main(String[] args) throws IOException
        {
                File src=new File("D:\\JAVA_基础班");//拷贝目录
                File dest=new File("D:\\新建文件夹");//复制目录
                copyTo(src,dest);

        }
        public static void copyTo(File src,File dest) throws IOException
        {
       
                File[] files=src.listFiles();
                String str1=src.toString().replaceAll("\\\\","\\\\\\\\");//目录字符串转义
                String str2=dest.toString().replaceAll("\\\\","\\\\\\\\");//目录字符串转义
                if(files==null)
                        return;
                for(File file:files)
                {
                        if(file.isDirectory())//是目录继续递归
                        {
                                copyTo(file,dest);
                        }
                        else if(file.isFile() &&file.toString().endsWith(".java"))//不是目录,切是文件,结尾为java开始复制
                        {       
                                String scc=file.getName().toString();
                                String ds=scc.substring(0,scc.length()-5);
                                 ds=ds+".txt";
                                BufferedInputStream bfis=new BufferedInputStream(new FileInputStream(file));//读取文件
                                BufferedOutputStream bfos=new BufferedOutputStream(new FileOutputStream(new File(str2,ds))); //重新写入,新地址目录+file的文件名
                                byte [] b=new byte[1024];
                                int len=-1;
                                while((len=bfis.read(b))!=-1)
                                {
                                        bfos.write(b,0,len);
                               
                                }
                                bfis.close();
                                bfos.close();
                        }       
                }
        }
}


2 个回复

倒序浏览
哇哦,看上去很厉害的样子
回复 使用道具 举报
都没有用过file的toString(),之前老师留了这个做作业,做了一晚上,,基础班要上完了,也忘得差不多了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马