Java source code examples

Java source code samples. Java code examples.

List all the contents of a directory

* * * * * 1 votes

The list method of File class can be used for listing all the contents of a directory.

import java.io.File;
/**
 * File class examples
 */
public class FileExample {
	public static void main(String[] args){
		// Create file object representing the directory
		File file = new File("d:\\temp");

		// List the contents of the directory.
		String contents[] = file.list();

		// Loop through the array and display the file/directory names.
		for (int i = 0; i < contents.length; i++) {
			System.out.println(contents[i]);
		}

	}
}

Tags: java.io

Discuss This Code