본문 바로가기

개발언어/JavaFunction

자바 람다식으로 텍스트 파일을 읽은 뒤 특정 텍스트 추출하기

현재 프로젝트의 분석 설계 단계에 들어가서 분석 설계 작업중이다. 이전 시스템 소스를 보고 해당 함수의 이름과 기능을 추출해야하는데 하나하나 소스를 열어서 해당 함수를 찾아서 드래그 한 뒤에 복사하기 보다는 프로그램으로 추출을 하자는 생각이 들어서 이번 프로그램을 만들게 됐다. 물론 다른 팀원들 중에서 엑셀의 매크로로 만드는 사람이 있는 것 같기는 한데 나는 자바가 좋아서 자바로 만들었다.

 

우선 예를 들면 다음과 같은 c프로그램 파일이 있다.

hello.c

extern int Update001();
extern int Update002();
extern int Update003();
extern int Update004();
extern int Update005();
extern int Update006();
extern int Update007();
extern int Update008();


/*******************************************************
***************Update001 업데이트를 한다.***************
********************************************************/
int Update001(){

}

/*******************************************************
***************Update002 업데이트를 한다.***************
********************************************************/
int Update002(){
}

/*******************************************************
***************Update003***************
********************************************************/

int Update003(){
}

/*******************************************************
***************Update004***************
********************************************************/
int Update004(){
}

 

 

test.c

extern int Select001();
extern int Select002();
extern int Select003();
extern int Select004();
extern int Select005();
extern int Select006();
extern int Select007();
extern int Select008();


/*******************************************************
***************SELECT001 조회를 한다.***************
********************************************************/
int Select001(){

}

/*******************************************************
***************SELECT002 조회를 한다.***************
********************************************************/
int Select002(){
}

/*******************************************************
***************SELECT003 조회를 한다.***************
********************************************************/

int Select003(){
}

/*******************************************************
***************SELECT004 조회를 한다.***************
********************************************************/
int Select004(){
}

 

 

 

여기서 함수의 목록과 주석에 달린 설명을 추출해야한다고 가정하면 소스는 다음과 같다.

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class Posting {
    public static void main(String[] args) throws IOException {
        List<String> fileList = new ArrayList<String>();
        Files.newDirectoryStream(Paths.get("C:\\DevelopTools\\IntelliJ\\src"), path -> path.toString().endsWith(".c")).forEach(path -> fileList.add(path.toString()));

        System.out.println("--------------파일 목록--------------");
        fileList.forEach(System.out::println);

        List<String> outputFile = new ArrayList<>();//텍스트를 담을 ArrayList
        for(String fileName : fileList){    //파일목록에서 파일 이름을 1개씩 가져온다.
            try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))) {  //해당위치의 파일을 읽는다.
                outputFile.add(fileName); //파일이름을 outputFile에 추출
                br.lines().filter(line ->
                                  line.contains("()") &&  //()가 포함된 라인이 && extren텍스트는 포함되지 않은 라인 || 한글이 포함된 라인
                                 (!line.contains("extern")) || line.matches(".*[ㄱ-ㅎㅏ-ㅣ가-힣]+.*"))
                        .map(line -> line.replace("{", "")) //{를 공백으로 치환
                        .forEach(line -> outputFile.add(line));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        System.out.println("파일 추출 후");
        outputFile.forEach(System.out::println);

        Files.write(Paths.get("c:/output.txt"), outputFile);

    }
}

 

 

추출된 내용

 

 

람다식이 어색한 사람들은 소스가 좀 낯설 것 같기는 하지만 읽어보면 이해는 충분히 될 것이다. 물론 아름다운 소스코드는 아니라고 생각된다. 나의 경우에는 최근에 람다식을 맛보니 재미가 있어서 억지로 람다식으로 만들어보느라 위와 같은 소스코드가 탄생했다. 위의 소스코드를 더 아름답게 만들어 주실 분이 있다면 소스코드를 작성 후 코맨트로 남겨주었으면 좋겠다.