How to list down the file and folder in Given directory

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

/*
 * Here we will learn to list down the content of a Directory
 */
public class ListDownContentOfDirectory {

	public static void main(String args[]) throws IOException {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter the file path");
		String filePath = scanner.next();

		File fileToTest = new File(filePath);

		if (fileToTest.isDirectory()) {
			System.out.println("List of files or folder in given directory");
			File[] listOfFiles = fileToTest.listFiles();

			for (File file : listOfFiles)
				System.out.println(file.getCanonicalPath());
		} else
			System.out
					.println("File with given absolute path is not a directory");
	}
}

Output of the Given Programme

Enter the file path
c:/jbt
List of files or folder in given directory
C:JBTSDK
C:JBTExcelSheet.xls
C:JBTFirstProgramme.class

 

By | 2012-09-21T19:27:47+00:00 September 21st, 2012|Code Base|0 Comments

About the Author:

Leave A Comment