简体   繁体   English

如何针对另一个文本文件的全部扫描文本文件的一行,然后从头开始针对同一文件扫描下一行?

[英]How to scan a line of a text file against the entirety of another text file, and then scan the next line against the same file from the beginning?

I'm new to Java so apologies in advance.我是 Java 的新手,所以提前致歉。

I need to scan through a.txt file where each line is a set of names, and if a name is present anywhere in another.txt file it should then output the line from the first file into a third.txt file.我需要扫描 a.txt 文件,其中每一行都是一组名称,如果名称存在于 another.txt 文件中的任何位置,那么它应该是 output 从第一个文件到 third.txt 文件的行。

As far as I am aware my current solution will only scan through the first line and then stop, because once scanB has reached the end of the file it cannot return to the beginning?据我所知,我当前的解决方案只会扫描第一行然后停止,因为一旦 scanB 到达文件末尾,它就无法返回到开头? So I probably need to use a completely different approach to achieve the result I'm looking for.所以我可能需要使用完全不同的方法来实现我正在寻找的结果。 The code I've got so far is below but I am aware it is most likely waaay off for what I need to be doing.到目前为止我得到的代码在下面,但我知道它很可能会因为我需要做的事情而中断。

Sorry again if there's any really really stupid mistakes in this, as I said I'm very new to this.如果这里有任何非常非常愚蠢的错误,再次抱歉,正如我所说的,我对此很陌生。

`File A = new File("A.txt");
 Scanner scanA = new Scanner(A);
 String personA = "";
 File B = new File("B.txt");
 Scanner scanB = new Scanner(B);
 String personCheck = "";
        
      while(scanA.hasNextLine()){
            personA = scanA.nextLine();
            while(scanB.hasNextLine()){
                personB = scaninteractionevents.nextLine();
            if(personCheck.contains(personB)){
                FileWriter f = new FileWriter("PersonList.txt", true);
                BufferedWriter b = new BufferedWriter(f);
                PrintWriter writer = new PrintWriter(b);
                writer.print(personCheck);
                }
       }       
}`

Thank you for asking your question.感谢您提出问题。 Being new is not a problem.成为新人不是问题。 Your question is clear and well provided with a reproducable example.您的问题很明确,并且提供了一个可重现的示例。

If you want to rescan a file multiple times, you need to reprovide the file to the scanner every time.如果要多次重新扫描文件,则需要每次都重新提供文件给扫描仪。 The best way to do this, is to make a new scanner every iteration.做到这一点的最好方法是每次迭代都制作一个新的扫描仪。

File A = new File("A.txt");
Scanner scanA = new Scanner(A);
String personA = "";
File B = new File("B.txt");
String personCheck = "";
        
while(scanA.hasNextLine()){
      personA = scanA.nextLine();
      Scanner scanB = new Scanner(B);
      while(scanB.hasNextLine()){
          personB = scaninteractionevents.nextLine();
          if(personCheck.contains(personB)){
             FileWriter f = new FileWriter("PersonList.txt", true);
             BufferedWriter b = new BufferedWriter(f);
             PrintWriter writer = new PrintWriter(b);
             writer.print(personCheck);
          }
       }
       scanB.close();
}
scanA.close();
f.close();
b.close();       

As you can see, I also added some close() calls, as it is good practice to close readers and writers to make sure your memory does not get flooded.如您所见,我还添加了一些 close() 调用,因为关闭读者和作者以确保您的 memory 不会被淹没是一种很好的做法。

Edit: as was said in the answers, it might be better not to reread the file every time you run the code.编辑:正如答案中所说,最好不要在每次运行代码时都重新读取文件。 You could indeed store it in a string, depending on the file size.您确实可以将它存储在一个字符串中,具体取决于文件大小。 This requires slightly more programming insights and is only necessary if you are working on efficient programming rather than starting to learn a new coding language.这需要更多的编程洞察力,并且只有在您致力于高效编程而不是开始学习新的编码语言时才有必要。 This is my opinion, others may disagree.这是我的看法,其他人可能不同意。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM