/* All Contributors (C) 2020 */
package io.github.dreamylost.practice;

import java.io.*;

/** @description 文件复制 */
public class Main12 {

    public static void main(String[] args) throws IOException {
        System.out.println("请依次输入：源文件路径 目标文件路径！");
        if (args.length < 2) {
            System.out.println("参数不合法！");
            System.exit(0);
        }
        String path = args[0];
        String target = args[1];
        String resultString = Main12.readFile(path);
        Main12.writeToFile(target, resultString);
    }

    public static String readFile(String path) throws IOException {
        File file = new File(path);
        if (!file.exists()) {
            System.out.println("源文件不存在！");
            System.exit(1);
        }
        try (BufferedReader br =
                new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"))) {
            char[] buf = new char[1024]; // 设置一个1024字符的缓冲区
            StringBuilder stringBuffer = new StringBuilder();
            try {
                while (br.read(buf) > 0) {
                    stringBuffer.append(buf);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return stringBuffer.toString();
        }
    }

    /**
     * @param path
     * @param result
     * @throws IOException
     * @description 复制的路径，和文件字节流
     */
    public static void writeToFile(String path, String result) throws IOException {
        File f = new File(path);
        try (PrintWriter outPrintWriter =
                new PrintWriter(
                        new BufferedWriter(
                                new OutputStreamWriter(new FileOutputStream(f), "UTF-8")))) {
            outPrintWriter.write(result);
            outPrintWriter.flush(); // 迫使缓冲区被输出
        }
    }
}
