• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to find a file (any file) in Java

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to find a file[any file] in whole computer system without passing path in JAVA , Please Help out.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jai .. I think it will surely help me out .. If not i will again raise question
 
karan chandok
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i did a simple google search and found this
 
karan chandok
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???

 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beware: "\\" will only work on DOS/Windows®. There is a static constant field in the File class which you should use instead.
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
karan chandok
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Karan,
Hope you tried well. Please see if below code works fine.
 
karan chandok
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi karan... the code just shows how to get all files... it never compare with input file. please have it written.
 
karan chandok
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Getting null Pointer Exception
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the stack trace. which line exception occurred? which object is null?
 
karan chandok
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exception in thread "main" java.lang.NullPointerException
at Path1.parseAllFiles(Path1.java:35)
at Path1.main(Path1.java:10)
 
karan chandok
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per above code null pointer exception in for parseAllFiles(f.getPath()) and(File f : filesInDirectory)

Please help
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI karan,

see if the additional IF condition resolves the NULL pointer exception



Refer Java doc on when listfiles() method return null
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic