使用 BufferedInputStream 和 Java 中的 RTL 字符将文本文件逐行解析为数组

发布于 2025-01-01 23:29:45 字数 2072 浏览 2 评论 0原文

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

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

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

这是一个代码片段:

            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一起使用的代码,但它非常慢!!!!!

谢谢。JadeYe

Guys I need to understand something:
the \n comes at the begining of a new line currect?
If so, I am trying to parse a file that has RTL characters in it and they are at the begining of a line, so:

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

when parsing the txt file (android from assets) I keep getting the first word from the next line concatenated to the Integer from the previous line.
I have tried everything but with no luck.

Here is a code snippet:

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

What I am trying to achieve is this:

Parse the text file really fast
Line by line into an array that is the inserted into a DB...

Any ideas???

Help is much appreciated as I've been at it for days now (loosing my hair :-()...

Currently I have code working with InputStreamReader but it is very slow!!!!!

Thank you.

JadeYe.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半枫 2025-01-08 23:29:45

使用:

BufferedReader in = new BufferedReader(
        new InputStreamReader(getAssets().open(fileName), "UTF-8"));
try {
    for (;;) {
        String line = in.readLine();
        if (line == null)
            break;
        ...
     }
} finally {
    in.close();
}

Use:

BufferedReader in = new BufferedReader(
        new InputStreamReader(getAssets().open(fileName), "UTF-8"));
try {
    for (;;) {
        String line = in.readLine();
        if (line == null)
            break;
        ...
     }
} finally {
    in.close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文