In: Computer Science
Write a java program to let the user enter the path to any directory on their computers. Then list all the files in that directory.
I have written the program using JAVA PROGRAMMING LANGUAGE.
INPUT:
OUTPUT :
CODE :
//imported the java.io.File
import java.io.File;
//Main Class
public class Main
{
static void FilesPrint(File[] arr,int index)
{
// To exit the recursive calls
if(index == arr.length)
return;
//to print the file/folder name on the console
System.out.println(arr[index].getName());
// Calling FilesPrint recursively by increasing the index value by one
FilesPrint(arr,++index);
}
// Driver Method
public static void main(String[] args)
{
// Provide full path for directory
//C:\\Users\\ponnada kumar\\Desktop\\Practice
String maindirpath = "input";
// File object
File maindir = new File(maindirpath);
if(maindir.exists() && maindir.isDirectory())
{
// To get the list Files in the given Directory
File arr[] = maindir.listFiles();
//Just a print statement
System.out.println("\nFiles/Folders from the given Directory : ");
// Calling FilesPrint
FilesPrint(arr,0);
}
}
}
Thanks..