【问题标题】:How to read a .txt file and split every 8 chars?如何读取 .txt 文件并每 8 个字符拆分一次?
【发布时间】:2018-01-22 21:40:01
【问题描述】:

我有一个包含“二进制”的文本文件,它只是一长串二进制数字,将代表未来的汇编指令。目前我想做的就是把这个长长的二进制数字符串加载到String[]一次8个字符中。

例如: 0000000100000002000000030000000400000005000000060000000700000008

将被读作:

[0] 000000001
[1] 000000002
[2] 000000003
[3] 000000004
[4] 000000005
[5] 000000006
[6] 000000007
[7] 000000008

我现在能想到的唯一方法是读入整个字符串并将其存储在 String 变量中,然后一次遍历变量 8 个字符并使用 substring() 来分割大串成更小的 8 个字符的字符串。

肯定有更有效的方法来做到这一点?

【问题讨论】:

  • 000000002 不是二进制
  • 它们是示例,我知道它们不是二进制字符串。
  • 感谢您的意见。祝你有美好的一天!

标签: java string io


【解决方案1】:
  1. 您可以使用 Alan Moore 在this 答案中提供的正则表达式,如果您更喜欢 Java 8 解决方案,您可能需要考虑使用 Stream

    String str = "0000000100000002000000030000000400000005000000060000000700000008";
    List<String> list = (Stream.of(str.split("(?<=\\G.{8})"))
                         .collect(Collectors.toList())); // Result of splitting input String is stored in a List
    String[] strings = list.toArray(new String[list.size()]); // Create an array from contents of list
    System.out.println(Arrays.toString(strings)); // print result array to console
    
  2. 如果不允许使用 Java 8 功能,可以使用 String 类中提供的方法 substring()

    String str = "0000000100000002000000030000000400000005000000060000000700000008";
    ArrayList<String> list = new ArrayList<>(); // To store results of cutting input String
    while(str.length() != 0) { // Until you reach end of String...
        list.add(str.substring(0, 8)); // Add first eight characters of input String
        str = str.substring(8, str.length()); // Cut input String to leave only characters not added to list in previous line
    }
    String[] array = list.toArray(new String[list.size()]); // Create a String[] and add contents of ArrayList to it
    System.out.println(Arrays.toString(array)); // Print result String[]
    

两种情况下的输出:

[00000001, 00000002, 00000003, 00000004, 00000005, 00000006, 00000007, 00000008]

【讨论】:

  • 优雅的答案,正是我想要的。谢谢!
  • @user9253826 很高兴我能帮上忙 :-)
【解决方案2】:

您可以拥有一个 8 字节的字节缓冲区,这样每次读取最多只能占用 8 个字符。您可以使用 String 构造函数轻松地从这个缓冲区形成一个字符串。

    char c[] = new char[8];
    List<String> strings = new ArrayList<>();
    try (FileReader reader = new FileReader(new File(""))) {
        while (reader.read(c) != -1) {
            strings.add(new String(c));
        }
    } catch (Exception e) {
        //Handle exception
    }

【讨论】:

    【解决方案3】:

    您可以创建一个Scanner 并继续“扫描”接下来的 8 个字符:

    Scanner s = new Scanner(new File(...));
    // you cannot use array here since you don't know how long the file is
    // you can convert this array list to an array afterwards though.
    ArrayList<String> list = new ArrayList<>();
    String eightChars;
    while ((eightChars = s.findWithinHorizon(".{8}", 0)) == null) {
        list.add(eightChars);
    }
    

    .{8} 是一个正则表达式,正好匹配 8 个非行终止符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2019-07-26
      • 2013-05-11
      相关资源
      最近更新 更多