统计Java项目中的所有Java源码输出到txt

1.代码展示

 /*
  * Copyright (c) 2021, 2023, yishun.cn All rights reserved.
  *
  */
 package cn.shun;

 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.util.concurrent.atomic.AtomicInteger;

 /**
  * <p>Project: JavaSE2022 - Ex02
  * <p>Powered by yishun On 2023-01-05 17:12:51
  *
  * @author yishun [2465734101@qq.com]
  * @version 1.0
  * @since 17
  */
 public class Ex02 {
     public static int count;
     //合并项目Java文件,统计输出,合并项目源码统计
     //System.getProperty("user.dir")为当前项目,输出到allCode.txt会自动在根目录创建
     public static void main(String[] args) {
         ok(new File(System.getProperty("user.dir")), new File("allCode.txt"));
     }

     public static void ok(File file, File dst) {

         if (file.isDirectory()) {
             for (File f : file.listFiles()) {
                 if (f.isDirectory()) {
                     ok(f, dst);
                 } else if (f.isFile() && f.getName().endsWith(".java")) {
                     count++;
                     add(f, dst, count);
                 }
             }
         }
         if (file.isFile() && file.getName().endsWith(".java")) {
             add(file, dst, 1);
         }
     }

     public static void add(File file, File dst, int count) {
         String txt = """
                 --------------------------------------------------------
                 No.%d
                 文件名:%s
                 行数:%d
                 时间:%tF %<tT
                 路径:%s
                 --------------------------------------------------------
                 %s
                 """;
         try (var fis = new FileInputStream(file); var fos = new FileOutputStream(dst, true)) {
             String src = new String(fis.readAllBytes());
             StringBuilder builder = new StringBuilder();
             AtomicInteger num = new AtomicInteger(1);
             src.lines().forEach(row -> {
                 builder.append(String.format("%d %s\n", num.getAndIncrement(), row));
             });
             //空两行
             builder.append("\r\n\r\n");
             String ok = String.format(txt, count, file.getName(), src.lines().count(), file.lastModified(), file.getAbsoluteFile(), builder);
             fos.write(ok.getBytes());
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
 }

2.效果如下

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值