package com.zjty.autotest.util;


import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * <p>Description : measurement
 * <p>Date : 2020/1/16 15:27
 * <p>@author : C
 */
@Slf4j
public class FileUtil {

    public final static String WORK_PATH = System.getProperty("user.dir") + "\\";

    public static void output(byte[] bytes, String path) {
        File file = new File(path);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        try {
            OutputStream output = new FileOutputStream(file);
            output.write(bytes);
            output.close();
        } catch (IOException e) {
            log.error("出现异常：" + e);
        }
    }

    public static void output(String text, OutputStream os) {
        byte[] buffer = new byte[1024];
        InputStream is = null;
        BufferedInputStream bis = null;
        try {
            is = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
            bis = new BufferedInputStream(is);
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
        } catch (Exception e) {
            log.error("error:" + e);
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
