package com.sysu.admin.utils.shape;

import java.io.*;

public class CommonMethod {

    public static void append2File(String file, String content, boolean isApppend) {
        FileWriter fw = null;

        try {
            //如果文件存在，则追加内容；如果文件不存在，则创建文件
            File f = new File(file);
            fw = new FileWriter(f, isApppend);
        }
        catch (IOException e) {
            e.printStackTrace();
        }

        PrintWriter pw = new PrintWriter(fw);
        pw.println(content);
        pw.flush();

        try {
            fw.flush();
            pw.close();
            fw.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getFileContent(String path) throws IOException {
        BufferedReader bufferedReader = null;
        StringBuffer result = new StringBuffer();
        try {
            FileInputStream inputStream = new FileInputStream(path);
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            while(bufferedReader.ready()){
                result.append(bufferedReader.readLine());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(bufferedReader != null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result.toString();
    }

}
