• Post Reply Bookmark Topic Watch Topic
  • New Topic

search a File in a directory  RSS feed

 
Roopam Samal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everyone.
to search a file algorithm seemed very easy to me
1>just give the file path
2>then get list of all files in the directory
3>check whether desired file is present in list or not

In my code I'm getting whole list of file in the given directory but match isn't found.can't understand why??




thanks in advance
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you shouldn't use the equality operator (==) to compare the contents of two String objects. Read our FAQ entry AvoidTheEqualityOperator for more information.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a lot easier way to find if a file exists
 
Roopam Samal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks stuart for telling the much easier way to search a file in a directory

In the previously written code if I use equals() instead of == it works fine as pointed out by Paul.
But i can't understand reason.


when compiler encounters string literal compiler checks if a identical string exists already or not if yes then reference to new literal is directed to existing string
so reason for it is:tt.txt is not already present in pool therefore this s[37]=="tt.txt" gives false. please rectify my mistake in understanding.


thanks in advance
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!