Here is a simple implementation of a folder explorer in Java:
import java.io.File; import java.util.Scanner; public class FolderExplorer { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter folder path: "); String folderPath = scanner.nextLine(); File folder = new File(folderPath); if (!folder.exists()) { System.out.println("Folder does not exist."); return; } if (!folder.isDirectory()) { System.out.println("Path is not a directory."); return; } System.out.println("Folder contents:"); File[] files = folder.listFiles(); for (File file : files) { System.out.println(file.getName()); } } }
This program takes a folder path as input from the user, checks if the folder exists and is a directory, and then prints the contents of the folder. The listFiles()
method of the File
class is used to obtain an array of File
objects representing the files and directories in the specified folder. The names of the files and directories are then printed using a for-each loop.
Note that this implementation only works for a single folder and does not traverse subdirectories.
I hope this helps!