2011-05-09 3 views
0

그래서 여기 내가 텍스트 파일을 읽을 것입니다 내 코드를 가지고 :하는 JList에로드 + 특정 정보를 추출 + A 형식의 텍스트 파일 읽기

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.FileNotFoundException; 
import java.io.IOException; 

public class ReadTextFile { 
    public static void main(String[] args) { 
     File file = new File("Test.txt"); 
     StringBuffer contents = new StringBuffer(); 
     BufferedReader reader = null; 

     try { 
      reader = new BufferedReader(new FileReader(file)); 
      String text = null; 

      // repeat until all lines is read 
      while ((text = reader.readLine()) != null) { 
       contents.append(text).append(System.getProperty("line.separator")); 
      } 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       if (reader != null) { 
        reader.close(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     // show file contents here 
     System.out.println(contents.toString()); 
    } 
} 

가 좋아, 지금은 내 텍스트 파일을 필요을 (TEST.TXT 내가 정말 필요한 것은

topic:- <climate>subtopic1, <atmosphere_type>subtopic2 

subtopic1:- <temperatures>sentence1, <gases>sentence2, <winds>sentence3 
subtopic2:- <gas_composition>sentence4, <gravitational_weight>sentence5 

sentence1:- temperatures are around 34ºC but they can reach -42ºC in winter 
sentence2:- there are significant proportions of nitrogen (13%) and butane (24%) 
sentence3:- there are permanent winds with gusts of 118 km/h 
sentence4:- methane (48%), nitrogen (13%), butane (24%) and oxygen (12%) 
sentence5:- gravity in ecuador is 7.95 atmospheres 

내가 다음 "기후"또는 "대기 유형")와 같이 항목을 (선택 수있는 최초의 를 가지고 :) 다음과 같은 구조 (예)가하기 내 초에 JList 하위 주제를 선택하겠습니다. "cl"을 선택하면 imate "를 선택하면"온도 ","가스 "또는"바람 "을 선택할 수 있습니다. 따라서 JButton을 누르면 해당 문장이 나에게 표시됩니다. 그런 것을하는 것이 어렵습니까? 당신의 도움을 주셔서 감사합니다! :)

+2

같은 질문을 다시 물어 보는 것은 허용되지 않는 동작입니다. 질문에주의를 환기시키려는 경우 이틀 후에 [현상금 배치] (http://stackoverflow.com/faq#bounty)가 허용됩니다. 또한 [질문을 편집하십시오] (http://stackoverflow.com/posts/5931649/edit)를 사용하여 추가 정보를 추가하면 질문을 이해하고 대답하기가 더 쉬워 질 수 있습니다. – Will

답변

0

파일에서 다음 데이터 구조를 빌드하십시오.

// map of a topic (or subtopic) name to a list of subtopic (or sentence) names 
Map<String, List<String>> subtopics = new HashMap<String, List<String>>(); 

// The above will contain the entries: 
// topic -> [subtopic1, subtopic2] 
// subtopic1 -> [sentence1, sentence2, sentence3] 
// subtopic2 -> [sentence4, sentence5] 

// map of topic/sentence name to heading 
Map<String, String> headings = new HashMap<String, String>(); 

// This will contain: 
// subtopic1 -> climate 
// subtopic2 -> atmosphere type 
// sentence1 -> temperatures 
// sentence2 -> gases 
// sentence3 -> winds 
// sentence4 -> gas composition 
// sentence5 -> gravitational weight 

// dictionary for looking up a sentence name and retrieving its corresponding text 
Map<String, String> dict = new HashMap<String, String>(); 

// sentence1 -> temperatures are around 34ºC but they can reach -42ºC in winter 
// sentence2 -> there are significant proportions of nitrogen (13%) and butane (24%) 
// sentence3 -> there are permanent winds with gusts of 118 km/h 
// sentence4 -> methane (48%), nitrogen (13%), butane (24%) and oxygen (12%) 
// sentence5 -> gravity in ecuador is 7.95 atmospheres 

이것은 좋은 방향으로 나아갈 것입니다. 주의해야 할 점은 처음으로 '하위 주제'맵에 주제를 추가 할 때 List (예 : ArrayList)를 먼저 작성하고 목록에 하위 주제 이름을 추가하고 목록으로 값을 추가해야한다는 점입니다 주제 이름은 항목 이름이 목록에 있으면 기존 목록에 하위 주제 이름을 추가하기 만하면됩니다.