콘솔로구

윤준석·2025년 4월 7일

package org.example;

import java.io.;
import java.nio.file.
;
import java.util.List;
import java.util.regex.;
import java.util.stream.
;

public class ConsoleLogExtractor {

private static final Pattern LOG_PATTERN = Pattern.compile("^\\s*console\\.log\\s*\\(.*?\\)\\s*;\\s*$");

public static void main(String[] args) throws IOException {
    String targetDir = "C:\\Users\\yjsyg\\OneDrive\\바탕 화면\\새 폴더 (2)"; // 수정: 처리할 디렉토리
    String extension = ".txt";    // 대상 확장자
    String outputFilePath = "extracted_console_logs.txt"; // 결과 저장

    try (BufferedWriter logWriter = Files.newBufferedWriter(Paths.get(outputFilePath))) {

        try (Stream<Path> files = Files.walk(Paths.get(targetDir))) {
            files
                    .filter(p -> p.toString().endsWith(extension))
                    .forEach(path -> {
                        try {
                            List<String> lines = Files.readAllLines(path);
                            List<String> cleanedLines = new java.util.ArrayList<>();

                            for (String line : lines) {
                                Matcher matcher = LOG_PATTERN.matcher(line);
                                if (matcher.matches()) {
                                    // 로그 저장
                                    logWriter.write("[" + path.toAbsolutePath() + "] " + line + System.lineSeparator());
                                } else {
                                    cleanedLines.add(line);
                                }
                            }

                            // 줄 삭제된 파일 다시 저장
                            //Files.write(path, cleanedLines);

                        } catch (IOException e) {
                            System.err.println("파일 처리 중 오류 발생: " + path + " → " + e.getMessage());
                        }
                    });
        }
    }

    System.out.println("완료: 로그는 '" + outputFilePath + "'에 저장되었습니다.");
}

}

0개의 댓글