明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
随笔 - 1277, 文章 - 0, 评论 - 214, 阅读 - 320万
  博客园  :: 首页  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

java书写并建立txt日志文件

Posted on   且行且思  阅读(229)  评论(0编辑  收藏  举报
复制代码
/*
     建立txt,并写入日志文件
     */
    public static void logWrite(String data, String name){
     //System.out.println("写入"+name+"开始");
        Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
//        int year = c.get(Calendar.YEAR);
//        int month = c.get(Calendar.MONTH);
//        int date = c.get(Calendar.DATE);
//        int hour = c.get(Calendar.HOUR_OF_DAY);
//        int minute = c.get(Calendar.MINUTE);
//        int second = c.get(Calendar.SECOND);
//        System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);

        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        data = "【写入时间:" + df.format(day) + "】:"+ data;
        byte[] sourceByte = data.getBytes();


        String path = getConfig("userfiles.basedir");
        FileOutputStream outStream =null;
        if(null != sourceByte){
            try {
                File file = new File(path + name );//文件路径(路径+文件名)
                if (!file.exists()) {   //文件不存在则创建文件,先创建目录
                    File dir = new File(file.getParent());
                    dir.mkdirs();
                    file.createNewFile();
                }
                outStream = new FileOutputStream(file,true); //文件输出流将数据写入文件
                outStream.write(sourceByte);
                String huanhang = System.getProperty("line.separator");
                outStream.write(huanhang.getBytes());

            } catch (Exception e) {
                e.printStackTrace();
                // do something
            }finally {
                try {
                    outStream.close();
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        }
    }
复制代码

 

调用:

 Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        FileUtils.logWrite(String.format("TableName:%s, 共有%s条数据." , "中国", 99) ,
                "/runlog/"+ df.format(day) + "/" + "999" +".txt");

 

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
历史上的今天:
2021-01-28 Windows下部署jar包启动服务后,过段时间出现服务假死、暂停或无反应现象
2010-01-28 Aptana :调试 javascript 图解
点击右上角即可分享
微信分享提示