在工作中使用log日志的时候,会对log日志进行生成和输入保存等操作,使用的都是java.io包内的方法。此处将常用的方法作下总结:
如果log名称为20180808.txt,则使用
public static void main(String[] args) {
String strFile="C:/Users/Administrator/Desktop/20180808.log";//日志文件路径及名称
File newFile=new File(strFile);//创建File对象
try {
newFile.createNewFile();//使用此方法创建新的20180808.log文件
//此时文件已经创建完成了,接下来就是读取和写入日志
BufferedOutputStream fo =new BufferedOutputStream(new FileOutputStream(newFile));
//读取日志文件,初始化缓冲流,以便接下来将要写入日志的内容缓存到内存中
String str="我是日志!!!";
fo.write(str.getBytes());//将日志内容转换为字节流写入缓存中
fo.flush(); //将缓存中的内容全部写入文件
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}