|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
How To Return Only Files NOT folders????
Hi,
The below method is used to return the contents of a directory. The problem I am having is my program is only interested in the actual files, he folders are irrelevant. Currently my program is reading-in a hardcoded file name and I am trying to make it read in any file. Can somebody be kind enough to suggest a way on how I can achieve this. Many Thanks Code:
public String[] filesList()
{
File dir = new File(System.getProperty("filelocation"));
String[] filesList = dir.list();
if(dir.exists())
{
System.out.println(filesList.length + " ..Files Found");
//for(int t = 0; t<filesList.length; t++)
//{
// System.out.println(filesList[t]);
//}
}
else
{
System.out.println("Directory not found");
mLogger.info("Directory not found");
}
return filesList;
}
}
|
|
#2
|
|||
|
|||
|
Filtering out the Directories
One way would be to create a class like:
Code:
import java.io.FilenameFilter;
import java.lang.String;
public class JustFiles implements FilenameFilter
{
public boolean accept( java.io.File file, java.lang.String g )
{
return !file.isDirectory();
}
};
Then in your code, you would have Code:
File dir = new File(System.getProperty("filelocation"));
JustFiles theFiles = new JustFiles();
String[] filesList = dir.listFiles( theFiles );
I hope this helps. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > How To Return Only Files NOT folders???? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|