aspose file tools*
The moose likes Java in General and the fly likes How to find a file (any file) in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to find a file (any file) in Java" Watch "How to find a file (any file) in Java" New topic
Author

How to find a file (any file) in Java

Antriksh Verma
Ranch Hand

Joined: Nov 24, 2008
Posts: 56
How to find a file[any file] in whole computer system without passing path in JAVA , Please Help out.
karan chandok
Greenhorn

Joined: Jul 15, 2011
Posts: 25
Antriksh Verma wrote:How to find a file[any file] in whole computer system without passing path in JAVA , Please Help out.




I also want to know it's solution Please help
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
At least i can tell you how to get the system drive. parseAllfiles() is the custom method you have to write to parse all the files & folders present in the system drive.


Karan - Welcome to the Ranch
Antriksh Verma
Ranch Hand

Joined: Nov 24, 2008
Posts: 56
Thanks Jai .. I think it will surely help me out .. If not i will again raise question
karan chandok
Greenhorn

Joined: Jul 15, 2011
Posts: 25
Jai

I am getting only the name of drive in which java is installed and not the all directories/drives

Please help
Antriksh Verma
Ranch Hand

Joined: Nov 24, 2008
Posts: 56
Karan is Right .. How to get all drives of system .. so that later we can search file in each drive Or any other suggestion instead of this .
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
i did a simple google search and found this
karan chandok
Greenhorn

Joined: Jul 15, 2011
Posts: 25
Thanks Jai

This method works and display all drives of system

Now if you could please help and tell me one thing how to search file in all locations and get
it's path???

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Beware: "\\" will only work on DOS/Windows®. There is a static constant field in the File class which you should use instead.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
1. Use the below code to get all system roots

As Campbell said use File.Separator wherever needed instead of "\\".

2. Now write the parseAllFiles(String parentFolder) method that will parse the folder and its subfolders.
a. Get all the files of the input parent folder using File.listFiles() method
b. Check if the file is a directory using File.isDirectory() method
c. If it is a directory then call parseAllFiles() method recursively
d. Else if it is a file check the file name with the file name you are searching for.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Antriksh Verma wrote:How to find a file[any file] in whole computer system without passing path in JAVA , Please Help out.



How will you know when you have the file you are looking for? For example, my computer has a large number of images all with the same name scattered across the file systems.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
karan chandok
Greenhorn

Joined: Jul 15, 2011
Posts: 25
File[] files = File.listRoots();
for (File f : files) {

System.out.println(f.getPath());

File f1 = new File(f.getPath()+"filename");


I am using this method,But the problem is, it is checking only the drives but not the folders or subfolders for the file

Do you have any idea how we can do that?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
karan chandok wrote:But the problem is, it is checking only the drives but not the folders or subfolders for the file
Do you have any idea how we can do that?

Yes I do. Follow below
2. Now write the parseAllFiles(String parentFolder) method that will parse the folder and its subfolders.
a. Get all the files of the input parent folder using File.listFiles() method
b. Check if the file is a directory using File.isDirectory() method
c. If it is a directory then call parseAllFiles() method recursively
d. Else if it is a file check the file name with the file name you are searching for.

And please use code tag to post code henceforth!
karan chandok
Greenhorn

Joined: Jul 15, 2011
Posts: 25



I am using above code to get the path of file from computer from all drives

But the problem is it is getting file from drive only but not from the folders or subfolders of drive

Can any one please give the solution of my problem?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Hi Karan,
Hope you tried well. Please see if below code works fine.
karan chandok
Greenhorn

Joined: Jul 15, 2011
Posts: 25
Hi Jai

Thanks for helping me again and again

Will you please tell me where to give the file name in the code you mentioned

Because I think I am making a mistake in that

Please
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
hi karan... the code just shows how to get all files... it never compare with input file. please have it written.
karan chandok
Greenhorn

Joined: Jul 15, 2011
Posts: 25


Getting null Pointer Exception
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Please post the stack trace. which line exception occurred? which object is null?
karan chandok
Greenhorn

Joined: Jul 15, 2011
Posts: 25
Exception in thread "main" java.lang.NullPointerException
at Path1.parseAllFiles(Path1.java:35)
at Path1.main(Path1.java:10)
karan chandok
Greenhorn

Joined: Jul 15, 2011
Posts: 25
As per above code null pointer exception in for parseAllFiles(f.getPath()) and(File f : filesInDirectory)

Please help
Arun Chidam
Ranch Hand

Joined: May 08, 2006
Posts: 79
HI karan,

see if the additional IF condition resolves the NULL pointer exception



Refer Java doc on when listfiles() method return null


SCJP 1.4, SCWCD 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find a file (any file) in Java