on 01-07-2009 1:05 PM
Hi All,
I'm trying to get all folder located on C:\ of server.
..lets say thre are 4 folders in C:\
thn I'm gettng those 4 folder of server..
but i want to do it all folder's name which thse 4 folders are containg on C:\
I'm tryng to use nested looping ,but m getting "null pointer exception" on statement:
rootfolder.listFiles()<i>.listFiles().length ;
using this line m trying to retrieve no of folders which are contained by secondary folder .
I hope m nt being too complicated.
Do any one hv any idea regarding it
hlpful posts will be appriciated.
Thanks n Regards
Khushboo
Hi Khushboo,
I agree it only returns directories directly under the initial directory.
You need to write the logic in a recursive function to get all the folders and sub-folders.
Kind Regards,
Nitin
PS: Updated sample code - hope it helps you
File directory = new File("directoryName");
listDirectories (directory);
void listDirectories(File directory)
{
// This filter only returns directories
FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}};
File[] files = directory.listFiles(fileFilter);
if(files.size() == 0)
System.out.println("Folder is = " + directory.getAbsolutePath());
else
for(int i=0; i<files.size(); i++)
listDirectories(files<i>);
}
Edited by: Nitin Jain on Jan 9, 2009 9:10 AM
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Still Could not find the exact answer for nested folders
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Khushboo,
As I understand, the said code will print the absolute path of all the files under the usr/... folder (You would have given 'null' as the extension parameter of the files for the same).
This will take some time as huge no. of files will be there.
Secondly, System.out will print in the Console view of the NWDS.
Thirdly, as i get your requirement is to print ONLY FOLDERS (not all the files), so you will need to make small changes to the code to get the appropriate collection (size of this will be very less).
Sample code - http://www.exampledepot.com/egs/java.io/GetFiles.html (search-engines)
Kind Regards,
Nitin
Edited by: Nitin Jain on Jan 8, 2009 12:19 PM
Edited by: Nitin Jain on Jan 8, 2009 12:42 PM
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi khushboo,
once remove the import statement and then try to reimport it, build your project gain and try.
because its working fine fir me.
Regrads
Narendra
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi both,
thanks for the reply.
@Narendra : I hv used ur jar file n reimport it.but still m getting same error.
@Kishan: m nt gettng calss not found exception. : but now its showing me loading only when i click
on action.
Its like i want d list of folders on WAS.. I think it shud b possible
Thanks n Regards
Khushboo
HI,
If you are working with a DC,
1:You need to create an 'External Library' project , include the .jar file into it , build and deploy into server.
2: Then add this project in WebDynpro references to your DC.
Also here is sample code to access folders in server at some location...
Use this if useful..
File f = new File("c:
");
File[] arr = f.listFiles();
int len = arr.length;
for(int i=0;i<len;i++)
{
if(arr<i>.isDirectory())
if(!(arr<i>.isHidden()))
wdComponentAPI.getMessageManager().reportSuccess(" "+arr<i>.getName());
}
Regards
Narayana
Hi Khushboo,
The error shows it is not getting the class FileUtils.
have you used the correct jar file.And added the jar files to your projects library.
i mean right click on your project name->Properties->java build path ->library->add external jar and then add the jar file .
Regards
Narendra
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Khushboo,
you can use the below code it will give you desired result.
i have already impleted it. so i can clarify all your doubts.
For this you need to use commons-io-1.3.2.jar which can be easily available over internet
import org.apache.commons.io.FileUtils;
int i=0;
File root = new File("C:/Xlf");
File file = null;
String[] extensions = { "xlf" };
boolean recursive = true;
//
// Finds files within a root directory and optionally its
// subdirectories which match an array of extensions. When the
// extensions is null all files will be returned.
//
// This method will returns matched file as java.io.File
//
Collection files = FileUtils.listFiles(root, extensions, recursive);
ArrayList arrayList = new ArrayList();
for (Iterator iterator = files.iterator(); iterator.hasNext();) {
String fileName = "file";
i++;
fileName = fileName +i;
arrayList.add(fileName);
file = (File) iterator.next();
System.out.println("File are = " + file.getAbsolutePath());
}
Regards
Narendra
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
94 | |
11 | |
11 | |
10 | |
9 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.