package com.energy.manage.service.util;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.Set;

/**
 * 对文件的处理
 */
public class FileUtil {

    /**
     * 写入txt文件
     *
     * @param result
     * @param fileName
     * @return
     */
    public static boolean writeDataHubData(Set<String> result, String fileName) {
        long start = System.currentTimeMillis();
        String filePath = "/Users/shiyue/Downloads/yedu";
        StringBuilder content = new StringBuilder();
        boolean flag = false;
        BufferedWriter out = null;
        try {
            if (result != null && !result.isEmpty() && StringUtils.isNotEmpty(fileName)) {
                fileName += "_" + System.currentTimeMillis() + ".txt";
                File pathFile = new File(filePath);
                if (!pathFile.exists()) {
                    pathFile.mkdirs();
                }
                String relFilePath = filePath + File.separator + fileName;
                File file = new File(relFilePath);
                if (!file.exists()) {
                    file.createNewFile();
                }
                out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
                for (String info : result) {
                    out.write(info);
                    out.newLine();
                }
                flag = true;
                System.out.println("写入文件耗时：*********************************" + (System.currentTimeMillis() - start) + "毫秒");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return flag;
        }
    }



}
