zoukankan      html  css  js  c++  java
  • 输出内容到文件(日志输出)

    package writetotextfile;
    
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    
    public class Write {
    
        public static void main(String[] args) throws IOException {
            String filePath = "E:/writetext8/log.txt";
            String file = filePath.substring(0, filePath.lastIndexOf("/"));
            File logfile = new File(file);
            if (!logfile.exists()) {
                logfile.mkdirs();
            }
            File log = new File(filePath);
            if (!log.exists()) {
                log.createNewFile();
            }
            String content = "
    
    请求来源ip为:
    127.0.0.1
    请求时间为:
    "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss-SSS").format(new Date())+"
    请求参数为:
    666";
            writeByFileWrite(filePath,content);
        }
        public static void writeByFileWrite(String _sDestFile, String _sContent)
                throws IOException {
            FileWriter fw = null;
            try {
                fw = new FileWriter(_sDestFile,true);
                fw.write(_sContent);
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                if (fw != null) {
                    fw.close();
                    fw = null;
                }
            }
        }
    
    }
  • 相关阅读:
    C语言的异常处理
    单例类模板
    智能指针模板
    数组类指针
    类模板
    函数模板
    shell 修改工作路径
    把目录C:Python34PCI_Codechapter2加到系统路径中
    twoSum
    归并排序
  • 原文地址:https://www.cnblogs.com/liqforstudy/p/5593321.html
Copyright © 2011-2022 走看看