編寫一個程序,把指定目錄下的所有的帶.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");
    }
    
}

來不及注釋了,睡覺,困死了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM