You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yudao/JavaFileExtractor.java

159 lines
5.6 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class JavaFileExtractor {
private static final int MAX_LINES = 3000;
private static final String OUTPUT_FILE = "extracted_java_code.txt";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要扫描的绝对路径: ");
String inputPath = scanner.nextLine().trim();
scanner.close();
Path rootPath = Paths.get(inputPath);
if (!Files.exists(rootPath) || !Files.isDirectory(rootPath)) {
System.err.println("错误:路径不存在或不是目录: " + inputPath);
return;
}
List<Path> javaFiles = new ArrayList<>();
try {
Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (file.toString().endsWith(".java")) {
javaFiles.add(file);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
System.err.println("遍历文件出错: " + e.getMessage());
return;
}
System.out.println("找到 " + javaFiles.size() + " 个Java文件");
// 拼接所有文件处理后的内容
List<String> allLines = new ArrayList<>();
int lineCount = 0;
for (Path file : javaFiles) {
if (lineCount >= MAX_LINES) {
break;
}
try {
List<String> processedLines = processJavaFile(file);
int remaining = MAX_LINES - lineCount;
if (processedLines.size() <= remaining) {
allLines.addAll(processedLines);
lineCount += processedLines.size();
} else {
allLines.addAll(processedLines.subList(0, remaining));
lineCount = MAX_LINES;
break;
}
} catch (IOException e) {
System.err.println("读取文件出错: " + file + " -> " + e.getMessage());
}
}
Path outputPath = rootPath.resolve(OUTPUT_FILE);
try (PrintWriter writer = new PrintWriter(
new OutputStreamWriter(new FileOutputStream(outputPath.toFile()), StandardCharsets.UTF_8))) {
writer.println("========== Java代码提取报告 ==========");
writer.println("扫描路径: " + inputPath);
writer.println("Java文件数: " + javaFiles.size());
writer.println("输出行数: " + Math.min(lineCount, MAX_LINES));
writer.println("限制行数: " + MAX_LINES);
writer.println("======================================\n");
for (String line : allLines) {
writer.println(line);
}
} catch (IOException e) {
System.err.println("写入输出文件出错: " + e.getMessage());
return;
}
System.out.println("处理完成!输出 " + Math.min(lineCount, MAX_LINES) + " 行到: " + outputPath.toAbsolutePath());
}
/**
* 处理单个Java文件:去除空行和注释,返回行列表
*/
private static List<String> processJavaFile(Path filePath) throws IOException {
List<String> result = new ArrayList<>();
List<String> lines = Files.readAllLines(filePath, StandardCharsets.UTF_8);
boolean inBlockComment = false;
for (String line : lines) {
String trimmed = line.trim();
if (inBlockComment) {
int endIndex = trimmed.indexOf("*/");
if (endIndex != -1) {
inBlockComment = false;
String after = trimmed.substring(endIndex + 2).trim();
if (!after.isEmpty()) {
result.add(after);
}
}
continue;
}
if (trimmed.contains("/*")) {
int startIdx = trimmed.indexOf("/*");
int endIdx = trimmed.indexOf("*/", startIdx + 2);
if (endIdx != -1) {
String before = trimmed.substring(0, startIdx).trim();
String after = trimmed.substring(endIdx + 2).trim();
String combined = (before + " " + after).trim();
if (!combined.isEmpty()) {
result.add(combined);
}
} else {
inBlockComment = true;
String before = trimmed.substring(0, startIdx).trim();
if (!before.isEmpty()) {
result.add(before);
}
}
continue;
}
if (trimmed.startsWith("//")) {
continue;
}
int commentIdx = trimmed.indexOf("//");
if (commentIdx != -1) {
trimmed = trimmed.substring(0, commentIdx).trim();
}
if (trimmed.isEmpty()) {
continue;
}
result.add(trimmed);
}
return result;
}
}