编写一个程序,把指定目录下的所有的带.java文件都拷贝到另一个目录中,拷贝成功后,把后缀名是.java的改成.txt。

package example;

import java.io.*;


public class Test {
    public static void main(String[] args) throws IOException {
        File file1=new File ("F:"+File.separator+"src");
        copyAndRename(file1);
    }

    private static void copyAndRename(File file1) throws IOException {
        File[] fi=file1.listFiles(new SuffixFilter());
        String filename="F:"+File.separator+"src2";
        byte[] buf=new byte[1024];
        int len;
        for(File f:fi){
            String fname=f.getName();
            BufferedInputStream bis=new BufferedInputStream(new FileInputStream(f));
            BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(filename+File.separator+fname));
            while((len=bis.read(buf))!=-1){
                bos.write(buf,0,len);
            }
             bis.close();
             bos.close();
           File f2=new File(f.getAbsolutePath());
           fname=fname.replace(".java", ".txt"); 
           File f3=new File("F:"+File.separator+"src"+File.separator+fname);
           f2.renameTo(f3);
        }
        
    }
}

class SuffixFilter implements FilenameFilter{

    public boolean accept(File dir, String name) {
        
        return name.endsWith(".java");
    }
    
}

来不及注释了,睡觉,困死了

posted @ 2016-02-24 00:37  zerocoin  阅读(2246)  评论(0编辑  收藏  举报