|
| |||||||||
| Tags: directory, files, java, listing, programming, subdirectories |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| ||||
| ||||
| Listing the Files or Subdirectories in a Directory
Hello, I am learning java programming language and while working on it, I want to know the details about the program from which I can able to get the List of the Files or Subdirectories in a Directory in java. If you are having any type of program which will help me then please provide that to me or if you are having any reference for getting it, then also please help me to achieve it. |
|
#2
| ||||
| ||||
| Re: Listing the Files or Subdirectories in a Directory
Hey, I have got the code below for getting the list of the files or subdirectories in a directory. So, you can able to make use of it. So, just understand and use it: Code: File directory = new File("dirname");
String[] str = directory.list();
if (str == null)
{
}
else
{
for (int i=0; i<str.length; i++)
{
String Fname = str[i];
}
} |
|
#3
| |||
| |||
| Re: Listing the Files or Subdirectories in a Directory
Hello, If you have got the list of files and then if you want to perform the sorting on it, then you need to make use of the code below: Code: FilenameFilter filenamefilter = new FilenameFilter()
{
public boolean takeit(File directory, String name)
{
return !name.startsWith(".");
}
};
children = directory.list(filenamefilter); |
|
#4
| ||||
| ||||
| Re: Listing the Files or Subdirectories in a Directory
Hello, I have following reference books on java, you can simply make use of it:
|
|
#5
| ||||
| ||||
| Re: Listing the Files or Subdirectories in a Directory
Hello, I think you must need to make use of the code below for getting the solution for your problem: Code: import java.io.Fileile;
import java.io.FileilenameFileilter;
import java.io.FileileFileilter;
public class ListFileilesSubs
{
private static void filelisting(String dirName)
{
System.out.println("Simple file listing...");
Fileile directory = new Fileile(dirName);
String[] str = directory.list();
printFileiles(str, dirName);
}
private static void doFileileFileilterListing(String dirName, String fileflter)
{
System.out.println("Fileile listing Fileilter");
final String fileFileilter = fileflter;
Fileile directory = new Fileile(dirName);
FileilenameFileilter flter = null;
if (fileFileilter != null)
{
flter = new FileilenameFileilter() {
public boolean accept(Fileile directory, String name) {
return name.startsWith(fileFileilter);
}
};
}
String[] str = directory.list(flter);
printFileiles(str, dirName);
}
private static void doFileileFileilterDirirectoryListing(String dirName) {
System.out.println("Fileilter Dirirectory listing...");
Fileile directory = new Fileile(dirName);
Fileile[] files = (new Fileile(dirName)).listFileiles();
FileileFileilter dirFileilter = new FileileFileilter() {
public boolean accept(Fileile directory) {
return directory.isDirirectory();
}
};
files = directory.listFileiles(dirFileilter);
for (int i=0; i<files.length; i++) {
System.out.println("[Dir] : " + files[i]);
}
System.out.println();
}
private static void printFileiles(String[] str, String dirName) {
if (str == null) {
System.out.println("Error in " + dirName);
} else {
for (int i=0; i<str.length; i++) {
String filename = str[i];
if ((new Fileile(dirName + Fileile.separatorChar + filename)).isDirirectory()) {
System.out.print("[Dir] : ");
} else {
System.out.print("[File] : ");
}
System.out.println(dirName + Fileile.separatorChar + filename);
}
}
System.out.println();
}
public static void main(String[] args) {
filelisting("Directory");
if (args.length == 0) {
doFileileFileilterListing("Directory", null);
} else {
doFileileFileilterListing("Directory", args[0]);
}
doFileileFileilterDirirectoryListing("Directory");
}
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Listing the Files or Subdirectories in a Directory" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to get the full path in directory listing | Ilampirai | Operating Systems | 5 | 13-04-2011 11:25 AM |
| Counting all files in a directory (and subdirectories) | Sarasija | Operating Systems | 4 | 22-08-2010 02:03 AM |
| Directory listing of a website | Gunner 1 | Software Development | 5 | 29-01-2010 02:01 PM |
| How to print Directory Listing in Windows | Foy | Windows Software | 5 | 14-09-2009 03:55 PM |
| Count all files in a directory tree including subdirectories in Windows XP | !const | Operating Systems | 3 | 29-06-2009 07:58 PM |