2021-05-17文件的拷貝

package demo01;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @qvthor liuwenzheng
 * @date 2021/5/13 17:32
 */
public class Demo01 {
    public static void main(String[] args) throws Exception{
        FileInputStream fi = null;
        FileOutputStream fo = null ;
        try {


            //1 準(zhǔn)備輸入輸出流
            fi = new FileInputStream("D:\\WeChatSetup.exe");
            fo = new FileOutputStream("C:\\des\\eChatSetup.exe");

            int len = 0;
            byte[] b = new byte[1024];
            while ((len = fi.read(b)) != -1) {
                fo.write(b, 0, len);
            }
        }catch (IOException e){
            System.out.println("文件路徑不存在");
        }finally {
            fi.close();
            fo.close();
        }

    }
}

Demo02

package demo01;

import java.io.*;

/**
 * @qvthor liuwenzheng
 * @date 2021/5/13 18:15
 */

public class Demo02 {
    public static void main(String[] args) throws IOException {


            BufferedInputStream fi = null;
            BufferedOutputStream fo = null ;
            try {

                //1 準(zhǔn)備輸入輸出流
                fi = new BufferedInputStream(new FileInputStream("D:\\WeChatSetup.exe"));
                fo = new BufferedOutputStream (new FileOutputStream("C:\\des\\eChatSetup.exe"));

                int len = 0;
                byte[] b = new byte[1024];
                while ((len = fi.read(b)) != -1) {
                  ;
                }
            }catch (IOException e){
                System.out.println("文件路徑不存在");
            }finally {
                fi.close();
                fo.close();
            }

        }
    }


Demo03

package demo01;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @qvthor liuwenzheng
 * @date 2021/5/13 18:24
 */
public class Demo03 {
    public static void main(String[] args) throws IOException {
        FileReader reader = new FileReader("D:\\WeChatSetup.exe");
        FileWriter writer = new FileWriter("C:\\des\\eChatSetup.exe");

        int len = 0 ;
        char[] buffer = new char[1024];
        while (((len = reader.read(buffer))) != 1){
            writer.write(buffer,0,len);
        }
        reader.close();
        writer.close();
    }
}

Main

package demo01;

import java.io.File;

public class Main {

    public static void main(String[] args) {
        // 展示文件樹
        File f = new File("d:/demo01");
        printTree(f, 0);
    }

    public static void printTree(File f, int level) {
        for (int i = 0; i < level; i++) {
            System.out.println("\t");
        }
        System.out.println(f.getAbsolutePath());
        if (f.isDirectory()) {
            level++;
            File[] strs = f.listFiles();
            for (int i = 0; i < strs.length; i++) {
                File f0 = strs[i];

                printTree(f0, level + 1);
            }
        }
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容