package com.uplooking.club.util;
import java.io.File;
import java.io.PrintWriter;

public class FileOperate {
    private String message;


    /**
     * 删除文件
     * @param filePathAndName 文本文件完整绝对路径及文件名
     * @return Boolean 成功删除返回true遭遇异常返回false
     */

    public static boolean delFile(String filePathAndName) {
        boolean bea = false;
        try {
            //"/upload/20170207153145"
            String filePath = filePathAndName;
            File myDelFile = new File(filePath);
            if(myDelFile.exists()){
                myDelFile.delete();
                bea = true;
            }else{
                bea = false;

            }
        }
        catch (Exception e) {

        }
        return bea;
    }


    /**
     * 有编码方式的文件创建
     * @param filePathAndName 文本文件完整绝对路径及文件名
     * @param fileContent 文本文件内容
     * @param encoding 编码方式 例如 GBK 或者 UTF-8
     * @return
     */
    public void createFile(String filePathAndName, String fileContent, String encoding) {

        try {
            //创建文件对象
            File myFilePath = new File(filePathAndName);
            //判断文件是否存在
            if (!myFilePath.exists()) {
                //创建文件
                myFilePath.createNewFile();
            }
            //创建文件，加编码，创建写对象
            PrintWriter myFile = new PrintWriter(myFilePath,encoding);
            //写对象 myFile对象写内容
            myFile.println(fileContent);
            //关闭写对象
            myFile.close();
        }
        catch (Exception e) {
            message = "创建文件操作出错";
        }
    }

}