How to search list of Files or Folders in given Folder in Java

Here we will learn to ist down all files and folder in Given Folder.


package com.io;

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

/*
 * Here we will learn to search files in Folder
 */
class SearchFileInFolder {

	public static void main(String[] args) throws IOException {
		System.out.println("Enter the path to folder to search for files");

		Scanner scanner = new Scanner(System.in);

		String folderPath = scanner.next();

		File folder = new File(folderPath);

		if (folder.isDirectory()) {
			File[] listOfFiles = folder.listFiles();
			if (listOfFiles.length < 1)
				System.out.println("There is no File inside Folder");
			else
				System.out.println("List of Files & Folder");

			for (File file : listOfFiles) {
                               if(!file.isDirectory())
				System.out.println(file.getCanonicalPath().toString());
			}
		}

		else
			System.out
					.println("There is no Folder @ given path :" + folderPath);
	}
}

	

Output of above code would be


Enter the path to folder to search for files
C:\JBT
List of Files
C:\JBT\AndroidSDK
C:\JBT\cygwin
C:\JBT\email.txt

	
By | 2017-07-22T23:08:01+00:00 May 19th, 2013|Code Base|1 Comment

About the Author:

One Comment

  1. Gaurav Tanwar January 5, 2016 at 5:16 am - Reply

    Nice artilce!!!

Leave A Comment