wood burning stoves
The moose likes Java in General and the fly likes Searching for files in an array of files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Searching for files in an array of files" Watch "Searching for files in an array of files" New topic

Searching for files in an array of files

Shamsudeen Akanbi
Ranch Hand

Joined: Dec 24, 2010
Posts: 83
Hi ranchers, I'm trying to create an application that searches through an array of files to see if it exists there. To start with, the file to be searched for is in an array. So i need to loop through the file array(to be searched for), while looping through, i use the current looped object to see if it exists in an array of Files. If it exists, i want to print the name of the file on the screen. Here's what i've done so far.

It just prints 'Not present 100' times. I've really searched through a lot of blogs but i just don't get it well on how to search for a file in an array in a file of an array. Thanks in advance!
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

The problem is you use "==" to compare names. See AvoidTheEqualityOperator to see why that is a bad idea.

Winston Gutkowski

Joined: Mar 17, 2011
Posts: 8942

Steve Luke wrote:The problem is you use "==" to compare names...

And furthermore, what you're trying to do is not simple. Even if you use equals(), it will only return true if the Files' abstract paths are the same; but I have no idea if, for example, you will be able to equate links with the files they point to.
I suspect also that you may run into differences in behaviour between OS's, even if the Files are common ones residing on a networked drive.

You might also want to have a look at the getAbsolutePath() method.


Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
Articles by Winston can be found here
I agree. Here's the link: http://aspose.com/file-tools
subject: Searching for files in an array of files
It's not a secret anymore!