2010-05-08 3 views
0

텍스트 파일을 String 변수로 읽으려고합니다. 텍스트 파일에는 여러 줄이 있습니다. 문자열을 인쇄하여 "read-in"코드를 테스트하면 모든 문자 사이에 공백이 추가됩니다. 문자 bigram을 생성하기 위해 String을 사용하기 때문에 공백으로 인해 샘플 텍스트가 쓸모 없게됩니다. 코드는FileInputStream을 사용하여 텍스트 파일을 String으로 읽는 문자열의 추가 공백

try { 
    FileInputStream fstream = new FileInputStream(textfile); 
    DataInputStream in = new DataInputStream(fstream);  
    BufferedReader br = new BufferedReader(new InputStreamReader(in)); 

    //Read corpus file line-by-line, concatenating each line to the String "corpus" 
    while ((strLine = br.readLine()) != null) { 
    corpus = (corpus.concat(strLine));  
    } 

    in.close(); //Close the input stream 
} 
catch (Exception e) { //Catch exception if any 
    System.err.println("Error test check: " + e.getMessage()); 
} 

어떤 조언을 해주셔서 감사합니다.

감사합니다.

답변

0

텍스트 파일은 인코딩 된 UTF-16 (유니 코드) 일 가능성이 큽니다. UTF-16은 각 문자를 나타내는 데 2 ​​바이트 또는 4 바이트를 사용합니다. 대부분의 서양 텍스트 파일의 경우 "중간"바이트는 인쇄 할 수 없으며 공백처럼 보입니다.

second argument of InputStreamReader을 사용하여 인코딩을 지정할 수 있습니다.

또는 (Windows의 메모장에서 .. 대화를 유닉스, 저장에의 iconv) 텍스트 파일을 수정 alt text