package shop.alien.util.txt;

import java.io.*;

/**
 * 读写txt文本文档
 *
 * @author ssk
 * @version 1.0
 * @date 2020/4/26 9:13
 */
public class TxtReadAndWrite {

    public static void main(String[] args) {
        //写入
        System.out.println(writeTxt("123456", "d://writeTxt.txt"));
        //读取
        System.out.println(readTxt("d://writeTxt.txt"));
    }

    /**
     * 读取文本文档:文本转String
     *
     * @param path 文本路径
     * @return 文本转String
     */
    private static String readTxt(String path) {
        if (path != null && !"".equals(path)) {
            StringBuilder sb = new StringBuilder();
            File file = new File(path);
            InputStreamReader inputStreamReader = null;
            BufferedReader bufferedReader = null;
            try {
                inputStreamReader = new InputStreamReader(new FileInputStream(file), "gb2312");
                bufferedReader = new BufferedReader(inputStreamReader);
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    sb.append(line);
                    sb.append("\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (inputStreamReader != null) {
                    try {
                        inputStreamReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (bufferedReader != null) {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return sb.toString();
        } else {
            return "";
        }
    }

    /**
     * 写入文本文档:String转文本
     *
     * @param content  原字符串
     * @param filePath 文件路径
     * @return 是否成功
     */
    public static boolean writeTxt(String content, String filePath) {
        boolean flag = true;
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter;
        try {
            File distFile = new File(filePath);
            if (!distFile.getParentFile().exists()) {
                distFile.getParentFile().mkdirs();
            }
            bufferedReader = new BufferedReader(new StringReader(content));
            bufferedWriter = new BufferedWriter(new FileWriter(distFile));
            //字符缓冲区
            char[] chars = new char[1024];
            int len;
            while ((len = bufferedReader.read(chars)) != -1) {
                bufferedWriter.write(chars, 0, len);
            }
            bufferedWriter.flush();
            bufferedReader.close();
            bufferedWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
            flag = false;
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return flag;
    }

}
