输出内容到文件(日志输出)

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;
            }
        }
    }

}
原文地址:https://www.cnblogs.com/liqforstudy/p/5593321.html