How can I get directory list from the server? With this code you can list all files in a given directory: Code: import java.io.File; public class ListFiles
public static void main(String[] args)
// Directory path here
String files;
for (int i = 0; i < listOfFiles.length; i++)
if (listOfFiles[i].isFile())
If you want to list only .txt files for example, Use this: Code: import java.io.File; public class ListFiles
public static void main(String[] args)
// Directory path here
String files;
for (int i = 0; i < listOfFiles.length; i++)
if (listOfFiles[i].isFile())
Note:
Alternative to test for files with a certain ending: Code: MyFilter implements FileFilter
Code: import java.io.File; public class ListFiles
public static void main(String[] args)
// Directory path here
String files;
for (int i = 0; i < listOfFiles.length; i++)
Technically, this code will display folders named hello.txt, or this is a directory.txt, but I think it's fairly safe to assume that most people won't name their directories this way.
Do you have a Java Problem?
Java Books
Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|