通过char读取文件char并在其中找到输入字符串

时间:2018-12-20 03:36:26

标签: java file bufferedreader

我有一个文件,需要逐个字符读取并找到输入字符串。我需要返回“输入字符串”出现在文件中的次数,但是我只需要一个字符一个字符地读取文件。

我想出了下面的代码,但在通过逐个读取char来找出如何在文件中查找字符串时遇到了问题。我先迭代for循环,然后在内部有while循环,但是如果char不匹配,那么我需要从for循环重新开始,我无法弄清楚该怎么做?

  public static void main(String[] args) throws IOException {
    String input = "hello world"; // "hello";
    handleFile(new File("some_file"), input);
  }

  private static int handleFile(File file, String input) throws IOException {
    int count = 0;
    try (BufferedReader br =
        new BufferedReader(new InputStreamReader(new FileInputStream(file),
            Charset.forName("UTF-8")))) {
      char[] arr = input.toCharArray();
      int r;

      // confuse here what logic I should have here?
      for (char a : arr) {
        while ((r = br.read()) != -1) {
          char ch = (char) r;
          if (ch == a) {
            break;
          }
        }
      }
    }

    return count;
  }

1 个答案:

答案 0 :(得分:3)

因此,从概念上讲,您需要保持匹配字符数的偏移量,每次发生不匹配时,请将偏移量重置回0。此偏移量用于确定输入中的给定字符是否与文件中的下一个字符

一个简单的实现可能看起来像...

String value = "Thistestistestatesttest";
String input = "test";

int offset = 0;
int matches = 0;
for (char next : value.toCharArray()) {
    if (next == input.charAt(offset)) {
        offset++;
        if (offset == input.length()) {
            matches++;
            offset = 0;
        }
    } else {
        offset = 0;
    }
}
System.out.println("Found " + matches);

请注意,我故意使用String作为源,因此您可以对其进行测试并更好地了解其工作原理,并采用自己的解决方案实施该逻辑。

现在,如果您花时间检查问题,则可能看起来像..

+======+========+==============+=======+x
| Next | offset | offset value | match |
+======+========+==============+=======+
| T    |      0 | t            | false |
+------+--------+--------------+-------+
| h    |      0 | t            | false |
+------+--------+--------------+-------+
| i    |      0 | t            | false |
+------+--------+--------------+-------+
| s    |      0 | t            | false |
+------+--------+--------------+-------+
| t    |      0 | t            | true  |
+------+--------+--------------+-------+
| e    |      1 | e            | true  |
+------+--------+--------------+-------+
| s    |      2 | s            | true  |
+------+--------+--------------+-------+
| t    |      3 | t            | true  |
+------+--------+--------------+-------+
| i    |      0 | t            | false |
+------+--------+--------------+-------+
| s    |      0 | t            | false |
+------+--------+--------------+-------+
| t    |      0 | t            | true  |
+------+--------+--------------+-------+
| e    |      1 | e            | true  |
+------+--------+--------------+-------+
| s    |      2 | s            | true  |
+------+--------+--------------+-------+
| t    |      3 | t            | true  |
+------+--------+--------------+-------+
| a    |      0 | t            | false |
+------+--------+--------------+-------+
| t    |      0 | t            | true  |
+------+--------+--------------+-------+
| e    |      1 | e            | true  |
+------+--------+--------------+-------+
| s    |      2 | s            | true  |
+------+--------+--------------+-------+
| t    |      3 | t            | true  |
+------+--------+--------------+-------+
| t    |      0 | t            | true  |
+------+--------+--------------+-------+
| e    |      1 | e            | true  |
+------+--------+--------------+-------+
| s    |      2 | s            | true  |
+------+--------+--------------+-------+
| t    |      3 | t            | true  |
+------+--------+--------------+-------+