简单的日志记录

    public static void method1(String text) {
        Date date = new Date();
        String format =  new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss").format(date);
        String formattow =  new SimpleDateFormat("yyyy-MM-dd").format(date);
        FileWriter fw = null;
        try {
            //如果文件存在,则追加内容;如果文件不存在,则创建文件

            File f=new File(log+"/log"+formattow+".txt");
            if(!f.exists()) {//如果文件夹不存在
                f.getParentFile().mkdirs();//创建文件夹
            }
            fw = new FileWriter(f, true);
        } catch (IOException e) {
            e.printStackTrace();

        }
        PrintWriter pw = new PrintWriter(fw);
        pw.println(format+":"+text);//写入时间和记录
        //ServerSocket.sendMessageAll(format+":"+text); //前端推送
        pw.flush();
        try {
            fw.flush();
            pw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
原文地址:https://www.cnblogs.com/broud/p/12400555.html