gpt4 book ai didi

java - 如何在Java中有效地读取带有分隔值的文本文件?

转载 作者:行者123 更新时间:2023-12-02 04:32:30 24 4
gpt4 key购买 nike

我有一个看起来像这样的文本文件:

2, 20, 0, 9, 7, 17, ... (100 columns)
1, 3, 22, 11, 0, 0, ...
...
(100 rows)

条件:

  1. 我想将每个值作为 int 读取到 int[][] 类型的变量中
  2. 这不是 csv 文件(文件中还有其他值)
  3. 这适用于 Android 开发 - 没有 Java 库
  4. 我希望它尽可能高效 - 最好根本不使用 String

我想到的,但不太喜欢:

while ((line = br.readLine()) != null) {
String[] values = line.split(','); // this heavily uses String
parseInts(values);
}

和:

while ((r = fileInput.read()) != -1) {
char c = (char) r;
if (c != ',' && c != '\n') {
doSomething(c); // this is problematic when reading 2 digit numbers
}
}

请记住,我想要一种有效的方法来做到这一点。任何帮助表示赞赏。谢谢

查看下面我的完整解决方案。

最佳答案

关于java - 如何在Java中有效地读取带有分隔值的文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31244565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com