wood burning stoves 2.0*
The moose likes Java in General and the fly likes Comparing contents of a file with String[] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparing contents of a file with String[]" Watch "Comparing contents of a file with String[]" New topic
Author

Comparing contents of a file with String[]

Eduardo Ponce de Leon
Ranch Hand

Joined: May 13, 2009
Posts: 99
Hi,
I am trying to compare the contents of an array to what ever there is in a file...so that
if - whatever i have in array[i] is in file
DO SOMETHING
else
DO SOMETHING

as of now i take the first element of my array and check it agains my file, however, when i find a match in my file i dont know how to reset my Buffer reader to start reading from the top of the file again and to check the next element in my array...here is the code





JUST SO YOU HAVE AN IDEA OF WHAT I HAVE IN MY .TXT FILE AND MY ARRAY.....

Data in File ----------------- Data in filesForBAT[]
crs_SP2010 ------------------ crs_SP2013.txt
usr_SP2010 ------------------ usr_SP2013.txt
crs_SP2011
usr_SP2011
crs_SP2012
usr_SP2012
crs_SP2013
usr_SP2012
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm not sure how this is any different than the original question you asked; if you can justify the apparent duplication I won't lock this thread.
Eduardo Ponce de Leon
Ranch Hand

Joined: May 13, 2009
Posts: 99
What do you mean, i just posted 1 time and edit the post a couple of times! What original question?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Eduardo Ponce de Leon wrote:Hi,
as of now i take the first element of my array and check it agains my file, however, when i find a match in my file i dont know how to reset my Buffer reader to start reading from the top of the file again and to check the next element in my array.


I wouldn't choose a method which required reading the whole file for each entry in the array. I would do it the other way around: read the file once, and for each line in the file, see if it matches an entry in the array. If it does, then do the something.

Your example doesn't show any duplicate entries in the file or the array, so that should be the same thing.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

http://www.coderanch.com/t/481592/Java-General/java/Comparing-array-elements-between-different
http://www.coderanch.com/t/481411/Java-General/java/Comparint-contents-files-different-folders
http://www.coderanch.com/t/481405/Beginning-Java/java/Comparing-data-files-different-folders
Eduardo Ponce de Leon
Ranch Hand

Joined: May 13, 2009
Posts: 99
oooobviously 2 different posts!!!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Not really.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40


Also, given the answers on how to compare two files, and the answers on how to compare to arrays, can't you just extrapolate it to how to compare a file and an array?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparing contents of a file with String[]