io流追加到一个文件中信息比如日志

package com.yh.day02.arrays;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileTest01 {

public static void main(String[] args) {
log("是冬至");//2019-12-22 11:22:08是冬至
}
public static void log(String msg) {
File file=new File("E:/upload/1.txt");
FileOutputStream fos=null;
PrintStream ps=null;
try {
fos=new FileOutputStream(file, true);
ps=new PrintStream(fos);
String time=getTime();
ps.print(time);
ps.print(msg);
ps.println();
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
if(ps != null){
ps.close();
}
}
}

public static String getTime() {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d=new Date();
return sdf.format(d);
}

}

你可能感兴趣的:(io流追加到一个文件中信息比如日志)