JAVA IO流十種拷貝檔案例子__JAVA

來源:互聯網
上載者:User
class IoTotal {    public static void main(String[] args) {    }    //字元輸入輸出資料流  一個一個讀    public static void total01() throws IOException {        FileReader fr = new FileReader("1.txt");        FileWriter fw = new FileWriter("2.txt");        int a;        while ((a = fr.read()) != -1) {            fw.write(a);        }        fr.close();        fw.close();    }    //字元輸入輸出資料流  一個字元數組讀    public static void total02() throws IOException {        FileReader fr = new FileReader("1.txt");        FileWriter fw = new FileWriter("2.txt");        char[] arr = new char[1024 * 4];        int len;        while ((len = fr.read(arr)) != -1) {            fw.write(arr, 0, len);        }        fw.close();        fr.close();    }    //高速字元輸入輸出資料流  一個一個讀    public static void total03() throws IOException {        FileReader fr = new FileReader("1.txt");        BufferedReader br = new BufferedReader(fr);        FileWriter fw = new FileWriter("2.txt");        BufferedWriter bw = new BufferedWriter(fw);        int a;        while ((a = br.read()) != -1) {            bw.write(a);        }        br.close();        bw.close();    }    //高速字元輸入輸出資料流  一個字元數組讀    public static void total04() throws IOException {        FileReader fr = new FileReader("1.txt");        BufferedReader br = new BufferedReader(fr);        FileWriter fw = new FileWriter("2.txt");        BufferedWriter bw = new BufferedWriter(fw);        char[] arr = new char[1024 * 4];        int len;        while ((len = br.read(arr)) != -1) {            bw.write(arr, 0, len);        }        bw.close();        br.close();    }    //字元緩衝流  裝飾方法  此處唯寫特有方法    public static void total05() throws IOException {        BufferedReader br = new BufferedReader(new FileReader("1.txt"));        BufferedWriter bw = new BufferedWriter(new FileWriter("2.txt"));        String str = null;        while ((str = br.readLine()) != null) {            bw.write(str);            bw.newLine();        }        br.close();        bw.close();    }    //位元組輸入輸出資料流  一個一個讀    public static void total06() throws IOException {        FileInputStream fin = new FileInputStream("1.txt");        FileOutputStream fout = new FileOutputStream("2.txt");        int a;        while ((a = fin.read()) != -1){            fout.write(a);        }        fin.close();        fout.close();    }    //位元組輸入輸出資料流 一個位元組數組的讀    public static void total07() throws IOException {        FileInputStream fin = new FileInputStream("1.txt");        FileOutputStream fout = new FileOutputStream("2.txt");        byte[] arr = new byte[1024*4];        int len;        while ((len = fin.read(arr)) != -1){            fout.write(arr,0,len);        }        fin.close();        fout.close();    }    //高速位元組輸入輸出資料流  一個一個讀    public static void total08() throws IOException {        FileInputStream fin = new FileInputStream("1.txt");        BufferedInputStream bin = new BufferedInputStream(fin);        FileOutputStream fout = new FileOutputStream("2.txt");        BufferedOutputStream bout = new BufferedOutputStream(fout);        int a;        while ((a = bin.read()) != -1){            bout.write(a);        }        bin.close();        bout.close();    }    //高速位元組輸入輸出資料流 一個位元組數組的讀    public static void total09() throws IOException {        FileInputStream fin = new FileInputStream("1.txt");        BufferedInputStream bin = new BufferedInputStream(fin);        FileOutputStream fout = new FileOutputStream("2.txt");        BufferedOutputStream bout = new BufferedOutputStream(fout);        byte[] arr = new byte[1024*4];        int len;        while ((len = bin.read(arr)) != -1){            bout.write(arr,0,len);        }        bin.close();        bout.close();    }    //位元組->字元->位元組    public static void total10() throws IOException {        BufferedReader br = new BufferedReader(new InputStreamReader(new BufferedInputStream(new FileInputStream("1.txt"))));        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream("1.txt",true))));        String line = null;        while ((line = br.readLine()) != null){            System.out.println(line);            bw.write(line);            bw.newLine();        }        br.close();        bw.close();       /* int len = 0;        char[] arr = new char[1024*4];        while ((len = br.read(arr)) != -1){            bw.write(arr,0,len);        }        bw.close();        br.close();*/    }}

聯繫我們

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