MFileUtil

创建时间:2018/1/9 14:30
更新时间:2018/1/9 14:30
标签:util

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class MFileUtil {
    public static void appendline(String filename, String str) {
        append(filename, str + "\r\n");
    }

    public static void appendDaily(String filename, String str) {
        appendDaily(filename, str, "utf-8");
    }

    public static void appendDaily(String filename, String str, String encoding) {
        filename = filename + CalendarToString(Calendar.getInstance()).replaceAll("-", "");
        File f = new File(filename);
        if (!f.exists()) {
            System.out.println("//Warning: [" + filename + "] not found. ");

            try {
                if (f.createNewFile()) {
                    System.out.println("//Warning: [" + filename + "]created. ");
                }
            } catch (IOException e) {
                System.out.println("//Exception: " + e.toString());
            }
        }

        if (!f.canWrite()) {
            System.out.println("//Error: [" + filename + "] can not write. ");
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(f, true);
            fos.write(str.getBytes());
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    private static String CalendarToString(Object object) {
        Calendar c = Calendar.getInstance();
        try {
            c = (Calendar) object;
        } catch (Exception e) {
            try {
                c.setTime(StringToDate((String) object));
            } catch (Exception e1) {
                return "";
            }
        }
        if (c == null) {
            return "";
        }
        Date d = c.getTime();

        String str_date = DateToShortString(d);
        return str_date;
    }

    private static Date StringToDate(String str_date) {
        if (str_date == null) return null;
        if (str_date.indexOf("-") > 0) {
            String arr_date[] = str_date.split("-");
            if (arr_date.length == 3) {
                if (arr_date[1].length() == 1) {
                    arr_date[1] = "0" + arr_date[1];
                }
                if (arr_date[2].length() == 1 || (arr_date[2].length() > 2 && arr_date[2].indexOf(" ") == 1)) {
                    arr_date[2] = "0" + arr_date[2];
                }
                str_date = arr_date[0] + "-" + arr_date[1] + "-" + arr_date[2];
            }
        }

        if (str_date.trim().length() == 10) {
            str_date += " 00:00:00";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
        Date d = new Date();
        try {
            d = sdf.parse(str_date);
        } catch (Exception e) {
            try {
                sdf = new SimpleDateFormat("yyyyMMdd", Locale.CHINA);
                d = sdf.parse(str_date);
                return d;
            } catch (Exception e1) {
                System.out.println(e1);
                return null;
            }
        }
        return d;
    }

    private static String DateToShortString(Date date) {
        return DateToString(date, "yyyy-MM-dd");
    }

    private static String DateToString(Date date_value, String dateformat) {
        String dateStr = "";
        if (date_value == null) return "";
        try {
            SimpleDateFormat DF = new SimpleDateFormat(dateformat);
            dateStr = DF.format(date_value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateStr;
    }

    public static void append(String filename, String str) {
        File f = new File(filename);
        if (!f.exists()) {
            System.out.println("//Warning: [" + filename + "] not found. ");
            try {
                if (f.createNewFile()) {
                    System.out.println("//Warning: [" + filename + "]created. ");
                }
            } catch (IOException e) {
                System.out.println("//Exception: " + e.toString());
            }
        }

        if (!f.canWrite()) {
            System.out.println("//Error: [" + filename + "] can not write. ");
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(f, true);
            fos.write(str.getBytes());
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    public static void write(String filename, String str) {
        write(filename, str, "utf-8");
    }

    public static void write(String filename, String str, String encoding) {
        File f = new File(filename);
        if (f.exists()) {
            System.out.println("//Warning: [" + filename + "] exist. rename to [/bak/" + f.getName() + "]. ");
            File f_bak = new File(f.getParent() + "/bak/");
            if (!f_bak.exists()) {
                f_bak.mkdir();
            }
            Calendar c = Calendar.getInstance();
            String d = c.get(1) + "_" + c.get(2) + "_" + c.get(5) + "_" + c.get(11) + "_" + c.get(12) + "_" + c.get(13);
            f_bak = new File(f.getParent() + "/bak/" + f.getName() + d);
            if (f_bak.exists()) {
                f_bak.delete();
            }
            f.renameTo(f_bak);
        }
        f = new File(filename);
        try {
            if (f.createNewFile()) {
                System.out.println("//Warning: [" + filename + "] created. ");
            }
        } catch (IOException e) {
            System.out.println("//Exception: " + e.toString());
        }

        if (!f.canWrite()) {
            System.out.println("//Error: [" + filename + "] can not write. ");
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(f);
            fos.write(str.getBytes(encoding));
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    public static String getFileContent(String filename) {
        return getFileContent(filename, "utf-8");
    }

    public static String getFileContent(String filename, String encoding) {
        StringBuffer sb = new StringBuffer();
        String read = "";
        File file = new File(filename);
        try {
            FileReader fileread = new FileReader(file);
            BufferedReader bufread = new BufferedReader(new InputStreamReader(new FileInputStream(filename), encoding));
            while ((read = bufread.readLine()) != null) {
                sb.append(read);
                sb.append("\r\n");
            }
        } catch (FileNotFoundException e) {
            System.out.print("Exception: File not found[" + filename + "]\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static boolean isExist(String filename) {
        File f = new File(filename);
        return f.exists();
    }

}