package org.diagbot.pub.utils;

import org.springframework.util.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: fyeman
 * @Date: 2018/6/26/026 11:51
 * @Description:
 */
public class FileReader {
    private static final String DEFAULT_PATH = "tc.dict";

    public List<String> readFileContents(File file) {
        InputStream is = null;
        List<String> fileContents = new ArrayList<String>(10);
        try {
            is = new FileInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"), 512);

            String theWord = null;
            do {
                theWord = br.readLine();
                if (theWord != null && !"".equals(theWord.trim())) {
                    fileContents.add(theWord.trim());
                }
            } while (theWord != null);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            this.close(is);
        }
        return fileContents;
    }

    public List<String> readFileContents(String path) {
        InputStream is = null;
        List<String> fileContents = new ArrayList<String>(10);
        try {
            if (StringUtils.isEmpty(path)) path = DEFAULT_PATH;
            is = this.getClass().getClassLoader().getResourceAsStream(path);
            if (is == null) {
                throw new RuntimeException(path + ".......文件未找到!!");
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"), 512);

            String theWord = null;
            do {
                theWord = br.readLine();
                if (theWord != null && !"".equals(theWord.trim())) {
                    fileContents.add(theWord.trim());
                }
            } while (theWord != null);
        } catch (IOException ioe) {
            System.err.println("读取文件" + path + "出错.......................");
            ioe.printStackTrace();
        } finally {
            this.close(is);
        }
        return fileContents;
    }

    public void writeFileContents(List<String> contents, String path) {
        this.writeFileContents(contents, path, "");
    }

    public void writeFileContents(List<String> contents, String path, String separator) {
        try {
            FileWriter fw = new FileWriter(path);
            int size = contents.size();
            for (int i = 0; i < size; i++) {
                fw.write(contents.get(i));
                if (i < size - 1) {
                    fw.write("\n");
                }
                if (!StringUtils.isEmpty(separator)) {
                    fw.write(separator);
                }
            }
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void close(InputStream is) {
        try {
            if (is != null) {
                is.close();
                is = null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
