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;
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>