简体   繁体   中英

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.

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.

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? 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.

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. ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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