I know I am fairly new to Developing with java but I feel you could change the line
if (files.endsWith(".txt") || files.endsWith(".TXT"))
with
if (files.toLowerCase().endsWith(".txt"))
and this way you can exclude the or statement ( || ) and allow for any possible variation in the extension, .txt / .Txt / .tXt / .tXT / .TXT / .TXt (I think that is all), I think it will make for cleaner and more defined code, and that way no matter how ever the casing is you can still get all the possible files with that extension. Let me know if I am wrong...But I love the example... Below is the changed code:

Originally Posted by
JavaPF
With this code you can list all files in a given directory:
import java.io.File;
public class ListFiles
{
public static void main(String[] args)
{
// Directory path here
String path = ".";
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
System.out.println(files);
}
}
}
}
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 path = ".";
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
if (files.toLowerCase().endsWith(".txt"))
{
System.out.println(files);
}
}
}
}
}
You can modify the .txt to be whatever file extension you wish.