2012-07-03 2 views
0

구분 된 파일로 변환해야하는 텍스트 파일 덤프가 있습니다. 파일이 같은 형식 (더 나은 단어의 부족에 대한) "기록"일련의 포함 : 자바, 나는이 파일을 줄 단위로 읽어 StringBuffer를 사용하고, 지금한 줄 및 여러 줄의 파일로 파일 구문 분석

User: abc123 
Date: 7/3/12 
Subject: the foo is bar 
Project: 123456 
Problem: foo bar in multiple lines of text 
Resolution: foo un-barred in multiple lines of text 

User: abc123 
Date: 7/3/12 
Subject: the foo is bar 
Project: 234567 
Problem: foo bar in multiple lines of text 
      which may include <newline> and 
      extend to multiple lines of text 
Resolution: foo un-barred in multiple lines of text 

... 

를 구문 분석 마지막 줄 바꿈 된 줄을 텍스트 파일로 출력하는 일련의 if(inputLine.toLowerCase().startsWith("user:")) 논리를 기반으로 한 개별 줄의 줄

그러나, 필드 ProblemResolution 무료 형태와 여러 줄이 될 수 있습니다. 나는 두 줄을 만드는 무언가를하려고합니다 : Problem: 다음에 오는 모든 줄을 추가하고 Resolution:에서 끝나는 줄을 추가하고 Resolution: 다음에 시작하여 Form:으로 끝나는 모든 줄을 추가하십시오.

내가 alerady StringBuilder 그러나, 나는 논리를 구성하는 방법을 아주 잘 모르겠어요 ...이 작업을 수행 할 수있는 적절한 방법이 될 수 있음을 시사 this linkthis link, 볼했습니다.

편집 : 나는 한 줄 한 줄을 읽고 있어요 때문에, 나는의 세 번째 줄이 있다면, 다음

<pseudocode> 
If the line starts with "Problem" extract the charactes after "Problem" else 
if the PRIOR line starts with "problem" and the current line doesnt start with "resolution" then append characters in line to prior line 
etc. 
</pseudocode> 

를 코딩하는 방법 주위에 내 머리를 포장하지만 힘든 시간을 보내고있어 "문제 ...? 난 그냥 작동하도록하는 방법을 시각화 할 수 없습니다.

어떤 아이디어 나 내 원하는 결과를 달성하는 다른 방법을?

+2

제안 된 로직이 상당히 견고합니다. 구현 방법은 무엇입니까? –

+0

@ nicholas.hauschild '어떻게이 두 문자열 사이의 모든 줄을 다른 줄에 끼워 넣을지'는 모릅니다. 질문을 명확히 할 것입니다. – dwwilson66

+0

당신의 입력이 잘 형성된다고 가정 할 수 있다면,이 논리는 확실합니다. –

답변

2

안녕을 그때 제대로이 라인을 따라 뭔가를 문제를 이해한다면해야 우와 RK :

StringBuilder problemDesc = new String....; 
    if(inputLine.toLowerCase().startsWith("problem:")){ 
     problemDesc.append(inputLine); 
     while(!inputLine.toLowerCase().startsWith("resolution:"){ 
      //read next line into inputline; 
      problemDesc.append(inputline); 
     } 
     //deal with problem description here and inputLine now has the line with 
     //Resolution in it Repeat same logic for retrieving the resolution value 
    } 
2
StringBuilder problem; 
StringBuilder resolution; 

//... 

// If the current line starts with "Problem: " 
if(inputLine.toLowerCase().startsWith("Problem: ")) { 
    // Continue appending to the string builder until the delimiting line is reached 
    while(!inputLine.toLowerCase().startsWith("Resolution") { 
     problem.append(inputLine); 
    } 
} 

// Something similar for resolution 
1

나는 여기에 조금 대담 될 것와 JavaCC와 같은 실제 파서 생성기의 사용을 제안하고있다.

당신은 자유형 인 두 필드 만 있음을 언급하고 있지만 앞으로 자유형으로 추가되는 필드가있을 수 있습니다. 다르게 처리 할 두 필드를 하드 코딩하면 세 번째, 네 번째 또는 네 번째 특별한 경우가 추가 될 때 많은 부작용이 발생할 수 있습니다.

JavaCC는 런타임에 추가 jar를 요구하지 않고 실제 구문 분석기를 생성하며, 더 나은 경우에는 향후 구문 분석 규칙에 대해 생각할 수있게되어 향후 특별한 문제가 발생하지 않을 것입니다.

+0

우. 그것은 나를 흥분시킨다. 많이. 오늘 오후에 다른 것을 탐험 해보세요. 감사! – dwwilson66