2017-11-22 4 views
-2

으로 변환하므로 개체 값을 문자열 배열로 변환하려고했습니다. 기본적으로 텍스트 파일에서 문자열을 가져 오기 위해 stringtokenizer를 사용하므로 특정 값을 검색 할 수 있습니다. 이 라이브러리를 처음으로 사용하고 있습니다. 그래서 어떤 도움을 주셔서 감사합니다. 여기 내 주요 방법입니다개체 값을 문자열 배열

public static void main(String[] args) { 
     String[] keys = {"she","sells","sea","shells","by","the","sea","shore"};  
     In in = new In("Protein.txt"); 
     TST<Integer> st = new TST<Integer>(); 
     String text1; 
     String string = ""; 
     String [] line1; 
     while ((text1 = in.readLine()) != null) { 

      String line = text1.trim(); 
      //System.out.println(line); 

      StringTokenizer stt = new StringTokenizer(line); 
      while (stt.hasMoreElements()) { 
       //System.out.println(stt.nextElement()); 
       string = String.valueOf(stt.nextElement()); 
       //line1 = string.split(""); 
       System.out.println(string);    
      }  
     } 
    } 
} 
+0

당신이 직면하고있는 어떤 오류? –

+0

오류가 없습니다. 개체 값을 'string = String.valueOf (stt.nextElement());')와 같이 문자열로 변환 할 수 있습니다. ' 내가해야 할 일은 객체 값을 문자열 배열로 변환하는 것이다. 하지만 어떻게해야할지 모르겠다. 검색했지만 도움을 찾을 수 없습니다. –

+0

Ok. 다른 옵션을 찾으십니까? 질문에 대해 더 자세히 설명해 주시겠습니까? –

답변

0

각 줄을 문자열로 읽는 동안 목록에 넣습니다. 모든 행을 읽은 다음 toArray를 호출 할 수 있습니다.

기타 옵션 파일 개체의 readAllLines을 사용할 수 있습니다.

0

Apache's FileUtils을 사용하면 File을 사용할 수 있습니다.

String content = FileUtils.readFileToString(new File(fileName), StandardCharsets.UTF_8); 

readLines 같은 더 유용한 방법이 있습니다, 대신 String[] 아래와 같이 List<String> 사용할 수 있습니다

List<String> lines = FileUtils.readLines(new File("myfile.txt"), Charsets.UTF_8);