• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Searching for files in an array of files

 
Shamsudeen Akanbi
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is you use "==" to compare names. See AvoidTheEqualityOperator to see why that is a bad idea.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic