读取文件的每一行并在java中搜索特定的单词

时间:2022-09-13 09:10:39

So I have an assignment that requires me to "Search a file line by line for a given string. The output must contain the line number, and the line itself, for example if the word files was picked the output look something like

所以我有一个赋值,要求我“逐行搜索一个给定字符串的文件。输出必须包含行号和行本身,例如,如果单词文件被选中,输出看起来像

5: He had the files
9: the fILEs were his

5:他有文件9:文件是他的

Code:

void Search(String input) throws IOException {
    int x = 1;
    FileReader Search = new FileReader(f);
    Scanner in = new Scanner(f);
    LineNumberReader L = new LineNumberReader(Search, x);
    StreamTokenizer token = new StreamTokenizer(Search);
    while (in.hasNextLine())
    {
        try
        {
            if (!in.findInLine(input).isEmpty())
            {
                display(Integer.toString(L.getLineNumber()) + ": " + L.readLine(), "\n");

                in.nextLine();
            }

        } catch (NullPointerException e)
        {
            System.out.println("Something Happened");
            in.nextLine();
        }
    }
}

So far there are 3 issues I need to figure out with my code.

到目前为止,我需要通过我的代码找出3个问题。

  1. As soon as instance occurs where the searched is not in a line, it immediately displays the next line, even though the searched word is not in the line, and then terminates from there without having displayed the rest of the lines that had the word in it.
  2. 一旦实例发生搜索不在一行中的情况,它会立即显示下一行,即使搜索到的单词不在行中,然后从那里终止,而不显示其中包含单词的其余行它。

  3. It supposed to display lines with the word, regardless of casing, but does not.
  4. 它应该显示带有单词的行,不管外壳,但不是。

  5. Preferably, it's supposed to display all of them at once, but instead is displaying line by line, until it errors out and terminates.
  6. 优选地,它应该一次显示所有这些,而是​​逐行显示,直到它出错并终止。

1 个解决方案

#1


2  

You're main problem is here...

你的主要问题是......

FileReader Search = new FileReader(f);
Scanner in = new Scanner(f);
LineNumberReader L = new LineNumberReader(Search, x);
StreamTokenizer token = new StreamTokenizer(Search);
while (in.hasNextLine())
{

You've basically opened two file readers against the same file, but you seem to be expecting them to know about each other. You advance the Scanner, but that has no effect on the LineNumberReader. This then messes up the reporting and line reading process.

你基本上打开了两个针对同一个文件的文件阅读器,但你似乎期望他们相互了解。您推进扫描仪,但这对LineNumberReader没有影响。这会混淆报告和读取过程。

Reading from Scanner should look more like...

从扫描仪阅读应该更像......

while (in.hasNextLine()) {
    String text = in.nextLine();

Having said that, I'd actually drop the Scanner in favor of the LineNumberReader as it will provide you with more useful information which you would otherwise have to do yourself.

话虽如此,我实际上放弃了Scanner,转而使用LineNumberReader,因为它将为您提供更多有用的信息,否则您必须自己做。

For example...

FileReader Search = new FileReader(new File("TestFile"));
LineNumberReader L = new LineNumberReader(Search, x);

String text = null;
while ((text = L.readLine()) != null) {
    // Convert the two values to lower case for comparison...
    if (text.toLowerCase().contains(input.toLowerCase())) {
        System.out.println(L.getLineNumber() + ": " + text);
    }
}

#1


2  

You're main problem is here...

你的主要问题是......

FileReader Search = new FileReader(f);
Scanner in = new Scanner(f);
LineNumberReader L = new LineNumberReader(Search, x);
StreamTokenizer token = new StreamTokenizer(Search);
while (in.hasNextLine())
{

You've basically opened two file readers against the same file, but you seem to be expecting them to know about each other. You advance the Scanner, but that has no effect on the LineNumberReader. This then messes up the reporting and line reading process.

你基本上打开了两个针对同一个文件的文件阅读器,但你似乎期望他们相互了解。您推进扫描仪,但这对LineNumberReader没有影响。这会混淆报告和读取过程。

Reading from Scanner should look more like...

从扫描仪阅读应该更像......

while (in.hasNextLine()) {
    String text = in.nextLine();

Having said that, I'd actually drop the Scanner in favor of the LineNumberReader as it will provide you with more useful information which you would otherwise have to do yourself.

话虽如此,我实际上放弃了Scanner,转而使用LineNumberReader,因为它将为您提供更多有用的信息,否则您必须自己做。

For example...

FileReader Search = new FileReader(new File("TestFile"));
LineNumberReader L = new LineNumberReader(Search, x);

String text = null;
while ((text = L.readLine()) != null) {
    // Convert the two values to lower case for comparison...
    if (text.toLowerCase().contains(input.toLowerCase())) {
        System.out.println(L.getLineNumber() + ": " + text);
    }
}