package com.cmcm.crashhandler;

import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/***
 ** @author nieyihe
 ** @email nieyihe@cmcm.com
 ** @date 2019/4/16
 **/
public class FileUtils {

    public static boolean appendFile(String filePath, String fileName, String content) {
        if(!TextUtils.isEmpty(filePath)) {
            mkdirs(filePath);
        }
        File file = new File(filePath + File.separator + fileName);
        if (file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file, true);
            byte[] bytes = content.getBytes("utf-8");
            outputStream.write(bytes);
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }

    public static void mkdirs(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
    }
}
