项目场景:
项目场景:软著获取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:\cdc\test”);
File target = new File(“F:\cdc\test_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();
}
}
2621





