• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

yxchun

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

JAVA 给过去一年每一天生成1个文件

 

获取一月中最大的天数:yearMonth.lengthOfMonth();
 public static void test002() throws IOException {
        // 定义年
        int year = 2024;
        String path ="C:\\Users\\computer\\Desktop\\2years\\"+year;
        // 定义12个月份
        for(int month=1;month<=12;month++){
            YearMonth yearMonth = YearMonth.of(year, month);
            // 获取一月中最大的天数
            int days = yearMonth.lengthOfMonth();

            String filename="";
            if(month<10){
                filename = filename+ "0"+month;
            }else {
                filename = filename+ month;
            }


            for(int i=1;i<=days;i++){
                String fileName002=filename;

                if(i<10){
                    fileName002 = fileName002+ "0"+i;
                }else {
                    fileName002 = fileName002+ i;
                }
                fileName002 +=".txt";
                // 创建文件并写入
                Files.write(Paths.get(path+fileName002),(path+fileName002).getBytes());
                System.out.println(path+fileName002);
            }
        }

    }

 

posted on 2025-03-05 15:14  yxchun  阅读(18)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3