0

我正在尝试将文本文件读入字符串变量。文本文件有多行。打印字符串以测试“读入”代码后,每个字符之间都有一个额外的空格。当我使用字符串生成字符二元组时,空格使示例文本无用。代码是

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());
}

我会很感激任何建议。

谢谢。

4

1 回答 1

0

您的文本文件可能是 UTF-16 (Unicode) 编码的。UTF-16 用两个或四个字节来表示每个字符。对于大多数西方文本文件,“中间”字节是不可打印的,看起来像空格。

您可以使用InputStreamReader 的第二个参数来指定编码。

或者,修改文本文件(Unix 上的 iconv,Windows 上记事本中的 Save As.. 对话框): 替代文字

于 2010-11-17T21:41:55.100 回答