【发布时间】:2011-09-28 17:36:03
【问题描述】:
我正在尝试从 .txt 文件中读取一些文本,这是我的代码:
String filePath = bundle.getString("filepath");
StringBuilder st = new StringBuilder();
try {
File sd = Environment.getExternalStorageDirectory();
File f = new File(sd, filePath);
FileInputStream fileis = new FileInputStream(f);
BufferedReader buf = new BufferedReader(new InputStreamReader(
fileis));
String line = new String();
while ((line = buf.readLine()) != null) {
st.append(line);
st.append('\n');
}
Log.i("egor", "reading finished, line is " + line);
} catch (FileNotFoundException e) {
Log.i("egor", "file not found");
} catch (IOException e) {
Log.i("egor", "io exception");
}
reader.setText(st.toString());
文字如下:
这是要测试的示例文本
.txt 文件是在 Windows 记事本中创建的。
这就是我得到的:
我的代码有什么问题?提前致谢。
【问题讨论】:
-
对我来说看起来像编码问题..将编码添加到新的 InputStreamReader(fileis)
-
@evilone,谢谢,我该怎么做?
-
尝试例如添加“UTF-8”或编码您用于在记事本中保存此文本文档的内容,作为 InputStreamReader 构造函数的第二个参数,文档 - download.oracle.com/javase/1.4.2/docs/api/java/io/…
标签: android text stream inputstream