1

我有一个文本文件,其中包含我需要预加载到 SQLite 数据库中的数据。我保存在 res/raw 中。

我使用 读取整个文件readTxtFromRaw(),然后使用StringTokenizer该类逐行处理文件。

但是,String返回的readTxtFromRaw不显示文件中的外来字符。我需要这些,因为有些文本是西班牙语或法语。我错过了什么吗?

代码:

String fileCont = new String(readTxtFromRaw(R.raw.wordstext));
StringTokenizer myToken = new StringTokenizer(fileCont , "\t\n\r\f");

readTxtFromRaw 方法是:

private String readTxtFromRaw(Integer rawResource) throws IOException
{
    InputStream inputStream = mCtx.getResources().openRawResource(rawResource);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int i = inputStream.read();
    while (i != -1)
    {
        byteArrayOutputStream.write(i);
        i = inputStream.read();
    }
    inputStream.close();

    return byteArrayOutputStream.toString();
}

该文件是使用 Eclipse 创建的,并且所有字符在 Eclipse 中都可以正常显示。

这可能与 Eclipse 本身有关吗?我设置了一个断点并在 Watch 窗口中检查了 myToken。我试图手动将奇怪的字符替换为正确的字符(例如 í 或 é),但它不会让我这样做。

4

1 回答 1

1

你检查过几种编码吗?

  • 你的源文件的编码是什么?
  • 你的输出流的编码是什么?

byteArrayOutputStream.toString()根据平台的默认字符编码进行转换。所以我猜它会去除外来字符或将它们转换为不显示在输出中的方式。

您是否已经尝试使用byteArrayOutputStream.toString(String enc)?尝试使用“UTF-8”或“iso-8859-1”或“UTF-16”进行编码。

于 2011-06-04T20:22:33.880 回答