Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding Folders  RSS feed

 
Devipriya Swaminathan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all ,
I have to find whether a particular folder exists or not in my local machine , if it presents then i need to find out in which drive it presents ?
I wrote a code using Java File system .
But it displays only the directories from C: drive.
So if i give any other folder name which is in someother drive ,it returns me false and more over it didnt search the subfolders too.
Can anyone please post some code???

import java.io.*;
import java.io.File;


import javax.swing.*;
import javax.swing.filechooser.*;


public class FindDirectory1
{
public static void main(String args[])
{

String s = new String("kokila");
File[] file = File.listRoots();
int n = file.length;
for(int i=0;i<n;i++)
{
System.out.println("Directories available : "+file[i]);

String[] fileNames = file[i].list();

for (int j=0; j<fileNames.length; j++)
{
System.out.println("FileNames:"+fileNames[j]);

if(fileNames[j].equals(s))
{
System.out.println("FOLDER EXISTS");
}
else
{


}

}

}

/*File[] files = directory.listFiles();
int x = files.length;
System.out.println(x);

int nFiles = 0;
for (int i = 0; i < files.length; i++)
{
if(files[i].isFile() == true)
nFiles++;
}
System.out.println(nFiles);
*/

}
}

Thanks is advance .
 
Devipriya Swaminathan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Friends,
Sorry for RePosting ...
But its really urgent.
Can anyone help me .
Only one drive is working . Not all the System drives. Please help me to find out the subfolders too , i came to know that we have to use recursive function to achieve that , but i dont know how.
Please help .

Thanks in Advance
 
sinasi susam
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear friend ,

you can use the exist() method of the File class in order to determine wheter its existing or not.

And if you want to know a File instance was a File or a Folder

you can use isFile() and isDirectory() methods of the file class.

good luck.
 
Devipriya Swaminathan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ma'am/Sir,
Thanks for your reply .
But i have used all the functions that you have mentioned .
isDirectory() , exists(),list() everyhting.
My problem is, it returns false for isDirectory ,eventhough it presents.
And it is not working for all the drives (for the C: drive it works perfectly not for other drives).
It returns the drive information but returning false for the isDrive() function.
I dont know how to write a recursive function for searching subfolders too to find out a particular folder is present?
Please help.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is hardly advanced Java. Moving to the "I/O and Streams" forum.
 
Hanuma Deepak Muvvala
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Devi,
by using recursion u can only search only C drive[drive os is installed in windows this is given by File.listRoot() method] but u cannot search remaining directories D,E,F....(I think...),if u want to recursively search in sub directories of c drive is Im giving pseudocode which may help u....

boolean isExists(File f){
if(f.isDirectory(){
File[] children = f.listFiles();
for(int i=0;i<children.length;i++){
if(children[i].isDirectory(){
//recirsive call
isExists(children[i]);}
else{
if(children.getName().equals("kokila"))
return true;
}
}//end for
}//end if
else{
if(children.getName().equals("kokila"))
return true;
}

}//end isExists


similarly if u want to test 4 others hard code d:\,e:\.....

hope u understand
 
Devipriya Swaminathan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear hanumadeepak muvvala ,

Thank you so much for your reply .
It is very useful and works fine.
Cant we do random search in all the directories C:,D: and etc., ?
If so how to achieve that?

Thanks in Advance.
 
sinasi susam
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friend (me mr)

Its posible to use a FileFilter to list Files



And its interesting if those methods worked only for C\ drive.

I havent experienced something like that.
Thats all i have
[ May 25, 2006: Message edited by: sinasi susam ]
 
Devipriya Swaminathan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Sir,
Thank u very much .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!