栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

软著获取java 源代码文件,输出txt

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

软著获取java 源代码文件,输出txt

项目场景:

项目场景:软著获取java 源代码文件,输出txt


package com.cdrundle;

import java.io.*;

/5用递归的方式得到所有工作空间中的.java文件,并输出service 实现类/
public class Lx5 {
public static void main(String[] args) throws IOException {
//这里写你需要的文件夹路径
File f = new File(“F:cdctest”);
File target = new File(“F:cdctest_service.txt”);

    BufferedWriter bw = new BufferedWriter(new FileWriter(target));
    StringBuilder sb = new StringBuilder();
    printPathAndLength(f, sb);
    write(sb.toString(), bw);
    System.out.println("结束大小:" + sb.length());
}

public static void printPathAndLength(File f, StringBuilder sb) throws IOException {

    if (f.isFile()) {
        if (f.getName().endsWith(".java") && !(f.getName().matches("(.*)Mapper(.*)"))) {
            if (f.getName().matches("(.*)ServiceImpl(.*)")) {
                if (f.length() != 0) {
                    sb.append(readFileToString(f));
                }
            }

        }
    } else {
        File[] arr = f.listFiles();
        if (arr.length > 0) {
            for (File f1 : arr) {

                if (f1.getName().equals("文件夹A")) {
                    continue;
                }
                if (f1.getName().equals("文件夹B")) {
                    continue;
                }
                if (f1.getName().equals("文件夹C")) {
                    continue;
                }
                if (f1.getName().equals("文件夹D")) {
                    continue;
                }
                if (f1.getName().equals("文件夹E")) {
                    continue;
                }

                printPathAndLength(f1, sb);
            }
        }
    }
}

private static void write(String str, Writer writer) {

    try {
        writer.write(str);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (writer != null)
                writer.close();
        } catch (Exception e2) {
            e2.printStackTrace();
        }

    }

}

private static String readFileToString(File file) {
    BufferedReader br = null;
    StringBuilder sb = new StringBuilder();
    try {
        br = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = br.readLine()) != null) {
            String s = line.trim();
            if (s.length() == 0) {
                continue;
            }
            if (s.startsWith("/") || s.startsWith("*")) {
                continue;
            }
            sb.append(line).append("n");
        }

    } catch (Exception e) {

        e.printStackTrace();

    } finally {

        try {

            if (br != null) {

                br.close();

            }

        } catch (Exception e2) {

            e2.printStackTrace();

        }

    }

    return sb.toString();

}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/936458.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号