0

伙计们,我需要了解一些事情: \n 出现在新行的开头?如果是这样,我正在尝试解析其中包含 RTL 字符并且它们位于行首的文件,因此:

  1. xxx xxxx, ABC DEFG, 1, 11, 111, 786
  2. xxx xxxx, ABC DEFG, 1, 11, 111, 786
  3. ETC...

在解析 txt 文件(来自资产的 android)时,我不断从下一行获取第一个单词,并从上一行连接到 Integer。我已经尝试了一切,但没有运气。

这是一个代码片段:

            InputStream is;
            try {
                is = new BufferedInputStream(getAssets().open(fileName));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                InputStream is = new BufferedInputStream(getAssets().open(fileName));
                byte[] c = new byte[128];
                byte[] d = null;
                int readChars = 0;
                int lineNumber = 0;
            String line;
                String[] paramLineArray = null;
                int k;
                while ((readChars = is.read(c)) != -1) {
                    for (int i = 0; i < readChars; i++) {
                        if (c[i] == '\n') {
                            lineNumber++;
                            line = new String(c);
                            k = 0;
                            StringTokenizer st = new StringTokenizer(line,",");
                            paramLineArray = new String[st.countTokens()];
                            while (st.hasMoreTokens()) {
                              // get next token and store it in the Line
                              paramLineArray[k] = st.nextToken();
                              k++;
                            }
                        }                       
                    }
                    publishProgress(((int) (1 / (float) lineNumber) * 100));
                    populateTables(paramLineArray, tblName, tblElements);
                }

我想要实现的是:

将文本文件真正快速地逐行解析为插入数据库的数组...

有任何想法吗???

非常感谢帮助,因为我已经做了好几天了(失去了我的头发:-()...

目前我有使用 InputStreamReader 的代码,但它非常慢!!!!!!

谢谢你。

玉叶。

4

1 回答 1

0

利用:

BufferedReader in = new BufferedReader(
        new InputStreamReader(getAssets().open(fileName), "UTF-8"));
try {
    for (;;) {
        String line = in.readLine();
        if (line == null)
            break;
        ...
     }
} finally {
    in.close();
}
于 2012-02-02T23:25:04.687 回答