如何读取.txt文件并分割每8个字符?

时间:2018-01-22 21:40:02

标签: java string io

我有一个包含“二进制”的文本文件,它只是一个很长的二进制数字串,它将代表下一步的汇编指令。目前,我想要做的是获取这一长串二进制数,并一次将其加载到String[]个8个字符中。

所以例如: 0000000100000002000000030000000400000005000000060000000700000008

将读作:

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

我现在能想到的唯一方法是读取整个字符串并将其存储在String变量中,然后一次迭代变量8个字符并使用substring()将大字符串切成较小的8个字符串。

当然必须有一种更有效的方法吗?

3 个答案:

答案 0 :(得分:2)

  1. 您可以在this答案中使用Alan Moore提供的正则表达式,如果您更喜欢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功能,则可以使用substring()类中提供的方法String

    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[]
    
  3. 两种情况下的输出:

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

答案 1 :(得分:0)

您可以创建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个非行终止符。

答案 2 :(得分:0)

您可以拥有一个8字节的字节缓冲区,这样每次读取最多只能占用8个字符。您可以使用String构造函数轻松地从此缓冲区形成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
    }