【发布时间】:2017-04-16 04:06:16
【问题描述】:
我有一个用 Windows 编辑器(或 Word)编写的标准文本文件,并以 ANSI 格式保存在 android 设备中。如果我打开并阅读此文件并将其显示在我的 Android 设备上,则所有字符都会正确显示,但德语变音符号 äÄöÖüÜß 除外。在黑色菱形中显示的是白色问号,而不是这些字符。 (我使用remoteViews.setTextViewText(...将它们显示在主屏幕小部件中)
我用谷歌搜索了几个小时,发现了很多关于使用 UTF-8 编码等的提示。但是当我以 UTF-8 或除 ANSI 以外的任何格式保存文件时,我会遇到异常并且无法读取文件全部。使用 android 编辑器显示文件的编码在 ANSI 和 UTF-8 中都是正确的。
我的程序太长,无法在此处复制,因此我提取了希望相关的部分并将其放在下面。请帮忙!
public class Test {
static void readFile() {
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "birthday.txt");
if (file.exists())
{
try {
FileReader fileReader = new FileReader(file);
BufferedReader br = new BufferedReader(fileReader);
//BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(directory+"birthday.txt"), "windows-1252"));
String lineOfText;
while ((lineOfText = br.readLine()) != null) {
//Output lineOfText via remoteViews.setTextViewText(WidgetOutput.getRef(linecounter).getIdWhat(), lineOfText);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
【问题讨论】:
-
读取 UTF-8 文件时遇到什么异常?
-
在这些阅读器的某个地方,您可以添加编码参数。试试看。
标签: java android file encoding ansi