編寫一個程式,將 d: \ java 目錄下的所有.java 檔案複製到d: \ jad 目錄下,並 將原來檔案的副檔名從.java 改為.jad

來源:互聯網
上載者:User

標籤:puts   替換   tor   Stub   row   class   瞭解   ati   write   

1.編寫一個程式,將 d: \ java 目錄下的所有.java 檔案複製到d: \ jad 目錄下,並 將原來檔案的副檔名從.java 改為.jad

 

package copy;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FilenameFilter;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class CopyTest {           public static void main(String[] args) throws Exception {            File file=new File("D:\\java");            //判斷目錄是否存在            if(!(file.exists()&&file.isDirectory())){                throw new Exception("目錄不存在");            }            // 找到檔案夾下面的 .java 檔案            File[] files=file.listFiles(new FilenameFilter() {                public boolean accept(File dir, String name) {                    // TODO Auto-generated method stub                    return name.endsWith(".java");                }            });            System.out.println(files.length);            // 複製檔案            // 找到目標目錄            File targetfile=new File("D:\\jad");            //判斷目錄是否存在,不存在建立            if(!(targetfile.exists()&&targetfile.isDirectory())){                targetfile.mkdirs();            }            // 替換檔案名稱字            for(File f:files){//                1.使用檔案流實現檔案複製                String targetFilename=f.getName().replace(".java", ".jad");//                FileInputStream in=new FileInputStream(f);//                FileOutputStream out=new FileOutputStream(new File(targetfile,targetFilename));//                copy(in,out);//                2.使用緩衝流加快讀寫效率                BufferedInputStream bis=new BufferedInputStream(new FileInputStream(f));                BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(new File(targetfile,targetFilename)));                CopyDemo2(bis,bos);            }                    }                      /**            * 使用檔案流實現檔案複製            * @author smy            * @throws Exception             */        public static void copy(InputStream in,OutputStream out) throws Exception{            byte[] data = new byte[1024*10];            int len = -1;            while((len = in.read(data))!=-1){                out.write(data,0,len);            }                            in.close();            out.close();        }                   /**         * 使用緩衝流加快讀寫效率         *          * 緩衝流是一對進階流,使用它們可以提高讀         * 寫效率。         *          * 進階流:進階流用來處理其他流,進階流不能         * 獨立存在,因為沒有意義,使用進階流的目的         * 是為瞭解決某些讀寫操作的複雜度。簡化我們         * 的讀寫操作。         *          * 低級流:資料來源明確,真實負責讀寫資料的流。         *          * 讀寫操作一定需要低級流,但不一定非要使用         * 進階流。         * @author SMY         * @throws Exception          */        public static void CopyDemo2(InputStream bis,OutputStream bos) throws Exception{            int d = -1;            /*             * 緩衝流內部維護了一個位元組數組作為             * 緩衝。當我們調用read()方法讀取一個             * 位元組時,緩衝流會一次性讀取若干位元組             * 並存入緩衝區,然後返回第一個位元組,當             * 我們再次調用read方法讀取一個位元組時,             * 它會立即將緩衝區中第二個位元組返回,             * 直到所有位元組都返回後,才會再次發生             * 實際的讀取操作,讀取一組位元組放入緩衝             * 區。             * 所以本質上還是通過一次讀取若干位元組,             * 減少讀取次數提高的讀取效率。             */            while((d=bis.read())!=-1){                bos.write(d);            }            bis.close();            bos.close();            }        }

 

編寫一個程式,將 d: \ java 目錄下的所有.java 檔案複製到d: \ jad 目錄下,並 將原來檔案的副檔名從.java 改為.jad

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.