package com.kfzs.duanduan.react;

import android.text.TextUtils;

import com.kfzs.duanduan.KFZSApp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/31.
 */

public class FileUtil {
    /**
     * 读取文件
     *
     * @param sFileName
     * @return
     */
    public static String readFile(String sFileName) {
        if (TextUtils.isEmpty(sFileName)) {
            return null;
        }

        final StringBuffer sDest = new StringBuffer();
        final File f = new File(sFileName);
        if (!f.exists()) {
            return null;
        }
        try {
            FileInputStream is = new FileInputStream(f);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            try {
                String data = null;
                while ((data = br.readLine()) != null) {
                    sDest.append(data);
                    sDest.append("\n");
                }
            } catch (IOException ex) {
                if (KFZSApp.DEBUG) {
                    ex.printStackTrace();
                }
            } finally {
                is.close();
                is = null;
                br.close();
                br = null;
            }
        } catch (Exception ex) {
            if (KFZSApp.DEBUG) {
                ex.printStackTrace();
            }
        } catch (OutOfMemoryError ex) {
            if (KFZSApp.DEBUG) {
                ex.printStackTrace();
            }
        }
        return sDest.toString().trim();
    }
    /**
     * 读取文件
     *
     * @param sFileName
     * @return
     */
    public static List<String> readLines(String sFileName) {
        if (TextUtils.isEmpty(sFileName)) {
            return null;
        }

        final List<String> list = new ArrayList<>();
        final File f = new File(sFileName);
        if (!f.exists()) {
            return null;
        }
        try {
            FileInputStream is = new FileInputStream(f);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            try {
                String data = null;
                while ((data = br.readLine()) != null) {
                    list.add(data);
                }
            } catch (IOException ex) {
                if (KFZSApp.DEBUG) {
                    ex.printStackTrace();
                }
            } finally {
                is.close();
                is = null;
                br.close();
                br = null;
            }
        } catch (Exception ex) {
            if (KFZSApp.DEBUG) {
                ex.printStackTrace();
            }
        } catch (OutOfMemoryError ex) {
            if (KFZSApp.DEBUG) {
                ex.printStackTrace();
            }
        }
        return list;
    }
    /**
     * 追加文件：使用FileWriter
     *
     * @param fileName
     * @param content
     */
    public static void addToFile(String fileName, String content) {
        try {
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            FileWriter writer = new FileWriter(fileName, true);
            writer.write(content);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
