java IO執行個體

來源:互聯網
上載者:User

標籤:static   關閉   close   imp   str   tin   目標   class   讀取   

import java.io.*;/** * Created by CLY on 2017/7/23. */public class Main {    public static void main(String[] arg){        testFile();        testFilenameFilter();        testInputStream();        testOutStream();        testCopyImg();    }    /**     * File類:     * 能代表一個特定檔案的“名稱”,也能代表“一個目錄下”的一組檔案的“名稱”。     */    public static void testFile(){        //作為一個“目錄列表器”使用        File file = new File("./");//目前的目錄        String list_url[] = file.list();        for (String one_url : list_url) {            System.out.println(one_url);        }    }    /**     * DirFilter介面:     * 該介面只規定了一個accept()方法。     * 我們可以使用匿名內部類的方式重寫該方法。     * 該方法的作用為:判斷傳入的string 檔案名稱,是否存在於傳入的目錄列表中     */    public static void testFilenameFilter(){        File file = new File("./");        FilenameFilter filenameFilter = new FilenameFilter(){            public boolean accept(File dir, String name) {                for (String file_name :                        dir.list()) {                    if (file_name.equals(name))                        return true;                }                return false;            }        };        //查看目前的目錄下是否存在"pom.xml"這個檔案        boolean result = filenameFilter.accept(file, "pom.xml");        System.out.println(result);    }    /**     * 所有的流都是站在“記憶體”的角度說的。     * 如“輸入資料流”,指的就是將其他媒介的流“輸入到本機記憶體的流中(這個本機記憶體的流就是輸入資料流)”。     * 然後使用者再從“記憶體中的”輸入資料流將資料讀出來。     *     * 輸出資料流的相關操作為:“將記憶體中的流(這個流指的就是輸出資料流)”傳到其他媒介的流中,     * 而我們需要做的,自然就是將資料“寫”到記憶體的輸入資料流中去,至於它是怎麼傳走的我們不用管。     */    /**     * InputStream和Reader。     *     * InputStream是所有“位元組”輸入資料流相關類的超類。     * InputStream介面中的read方法提供“對位元組的讀取(byte)”     *     * Reader是所有用於“讀取字元流”的相關類的超類     * Reader介面中的read方法提供“對字元的讀取(char數組或者String)”     *     * 而我們操作時自然是操作字元更順暢。     * 實際上InputStream的read方法一般也不是給開發人員用的,而是給其他類使用的。     * 所以可以看出,整個流程為:     * 先擷取到InputStream介面類型的“位元組流”資料。     * 然後轉化成reader介面類型的“字元流”資料。     * 最後調用reader介面提供的reader方法讀出字元。     */    public static void testInputStream(){        //擷取本地“檔案名稱”的對象        File file = new File("./pom.xml");        try {            //FileInputStream是InputStream的一個子實作類別,可以從指定檔案中讀取位元組流存到記憶體中的“InputStream(輸入位元組流)”中            FileInputStream inputstream = new FileInputStream(file);            /**             * InputStreamReader是Reader的一個子實作類別             * 是位元組流通向字元流的橋樑,“封裝”了InputStream在裡頭,             * 它以較進階的方式,reader()一次讀取一個一個字元,預設讀取的是字元的ASCII碼             */            InputStreamReader reader = new InputStreamReader(inputstream);            int ch;            //一次讀取一個字元的ASCII碼,如果為-1,就表示結束了。            while ((ch = reader.read())!=-1){                System.out.print((char)ch);            }            System.out.println();            /**             * BufferedReader             * 對之前的普通的InputStreamReader的又一次“裝飾”             * 提供緩衝的方式讀取文本,通過readLine()方法可以一次讀取一行,效率高。             */            FileInputStream inputstream2 = new FileInputStream(file);            InputStreamReader reader2 = new InputStreamReader(inputstream2);            BufferedReader bufferedReader = new BufferedReader(reader2);            String line;            while ((line = bufferedReader.readLine())!=null){                System.out.println(line);            }            bufferedReader.close();            reader2.close();            inputstream2.close();            reader.close();            inputstream.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }    /**     * OutputStream和Writer     *     * 與上面同理     * OutputStream負責將“記憶體中的輸出資料流”輸出,     * 然後也提供給我們一個writer方法,讓我們把“位元組(byte)”寫入記憶體中的輸出資料流。     *     * 而Writer提供的writer方法則是讓我們把“字元(char數組或者String)”寫入記憶體中的輸出資料流。     *     * 注意上面的描述,通過write()方法只能將資料寫入“記憶體裡的輸出資料流”(相當於緩衝起來)。     * 必須得通過flush()方法,才能將所有記憶體中緩衝的資料發送給目的地。     */    public static void testOutStream(){        //在目前的目錄建立一個新檔案        File new_file =new File("./test.txt");        try {            if (!new_file.exists()){                new_file.createNewFile();            }            /**             * FileWriter的write(String)方法已經可以將字串寫入輸出資料流了。             * 可此處通過“裝飾類”BufferedWriter裝飾Writter的好處是:             * 加了一個緩衝,緩衝寫滿後再將資料寫入硬碟             * 這樣做極大的提高了效能             * 如果單獨使用FileWriter也可以,             * 但你每寫一個資料,硬碟就有一個寫動作,效能極差             */            String out_words = "hello world";            //true表示對檔案再次寫入時,會在該檔案的結尾續寫,並不會覆蓋掉。            FileWriter fileWriter = new FileWriter(new_file,true);            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);            bufferedWriter.write(out_words);            bufferedWriter.flush();            bufferedWriter.close();            fileWriter.close();        } catch (IOException e) {            e.printStackTrace();        }    }    /**     * 執行個體:     * 依靠位元組流傳輸複製圖片     */    public static void testCopyImg(){        //待覆制的檔案地址        File file_old = new File("./old.jpg");        //新檔案地址        File file_new = new File("./new.jpg");        //讀取檔案到輸入字元流中,然後再從輸入字元流寫入輸出字元流,然後由輸出字元流寫到目標檔案        try {            if (!file_new.exists()){                file_new.createNewFile();            }            FileInputStream fileInputStream = new FileInputStream(file_old);            FileOutputStream fileOutputStream = new FileOutputStream(file_new);            //建立緩衝位元組數組            byte data[] = new byte[1024];            // 一次性讀入1024個位元組,即讀入到位元組數組中,然後再將數組寫入輸出資料流            while ((fileInputStream.read(data))!=-1){                fileOutputStream.write(data);                fileOutputStream.flush();            }            // 依次 關閉流(先開後關,後開先關)            fileOutputStream.close();            fileInputStream.close();        } catch (IOException e) {            e.printStackTrace();        }    }}

 

java IO執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.