Sample code for Listing file names in a directory


also read:

public String[] getFileNames(String DirPath) {
	String directoryName;
	File directory;
	String[] files;
	directory = new File(DirPath);
	if (directory.isDirectory() == false) {
		if (directory.exists() == false) {
			System.out.println("There is no such directory!");
			return null;
		} else {
			System.out.println("That file is not a directory.");
			return null;
	} else {
		files = directory.list();
		// System.out.println("Files in directory -" + directory + "/:");
		for (int i = 0; i < files.length; i++) {
		//System.out.println("" + files[i]);
		return files;



About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind


Please support the site
By clicking any of these buttons you help our site to get better