Because I asked wrong question last time, I want to correct my intention. How can I find file by name in specified folder? I have a variable with a name of this file and i want to find it in specified folder. Any ideas?
Asked
Active
Viewed 468 times
2 Answers
5
Maybe the simplest thing that works is:
String dirPath = "path/to/directory";
String fileName = "foo.txt";
boolean fileExistsInDir = new File( dirPath, fileName ).exists();
File is just a placeholder for a location in the file system. The location does not have to exist.

jackrabbit
- 5,525
- 1
- 27
- 38
-
This is cool - it's funny I didn't think of it earlier. However, this approach won't work if you need to search recursively. – AbdullahC Nov 12 '11 at 18:01
-
Sure, recursive searching is more involved. The question does state "find by name in specified folder" though. – jackrabbit Nov 12 '11 at 18:08
-
"find by name in specified folder" reminds me of the Unix `find /folder -name 'file_name'` ;) – AbdullahC Nov 12 '11 at 18:11
-
Indeed, but doing that correctly from Java would be a lot of work :) – jackrabbit Nov 12 '11 at 18:34
2
Use Finding files in Java as a starting point. It should have everything that you are looking for - ask another specific question if you get stuck.

AbdullahC
- 6,649
- 3
- 27
- 43