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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 伊乐杰 中级黑马   /  2016-4-8 20:18  /  324 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package com.heima.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test5 {
        /**
         * 编写一个程序,把一个目录里面所有后缀名是.java的文件改为.txt文件,然后拷贝到另一个目录去
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
                File src = new File("E:\\a");
                File desc = new File("E:\\b");
                copyTo(src,desc);
        }
        /*
         * 先改名再拷贝的方法
         * 1,返回值类型                void
         * 2,参数列表                        File src, File desc
         */
        public static void copyTo(File src, File desc) throws IOException {
                File[] files = src.listFiles();                                                                                                                                                //将文件存储到File数组中
                for(File subFile : files) {                                                                                                                                                        //遍历数组
                        if(subFile.isDirectory()) {                                                                                                                                        //如果是文件夹
                                copyTo(subFile, desc);                                                                                                                                        //递归调用
                        } else if(subFile.isFile() && subFile.getName().endsWith(".java")) {                                //如果是文件并且后缀名是.java
                                //subFile.getName().replace(".java", ".txt");                                                                                        //后缀名改为.txt
                                File newFile = new File(desc, subFile.getName().replace(".java", ".txt"));                                                                //在目的路径创建一个新文件,名字与当前文件名相同
                                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile));                                                        //创建高效输入输出流
                                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
                                /*byte[] arr = new byte[1024 * 8];
                                int len;
                                while((len = bis.read(arr)) != -1) {
                                        bos.write(arr);
                                        bos.flush();
                                }*/
                                int b = -1;
                                while(-1!=(b=bis.read())) {
                                        bos.write(b);
                                }
                                bis.close();
                                bos.close();
                        }
                }
        }
}

1 个回复

倒序浏览
因为要复写成.txt文件所以建议用字符流
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马