Skip to content

Instantly share code, notes, and snippets.

@Ensamisten
Created November 6, 2023 03:32
Show Gist options
  • Select an option

  • Save Ensamisten/cff8752f8ea6ad773a193c97dd326b74 to your computer and use it in GitHub Desktop.

Select an option

Save Ensamisten/cff8752f8ea6ad773a193c97dd326b74 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.util.Arrays;
public class Main {
private static final String INDENT = " "; // Indentation for tree structure
public static void listFilesAndFolders(String directoryPath, String prefix) {
File directory = new File(directoryPath);
// Get all files and folders in the current directory
File[] files = directory.listFiles();
if (files != null) {
// Sort the files and directories alphabetically
Arrays.sort(files);
for (int i = 0; i < files.length; i++) {
String treeSymbol = (i == files.length - 1) ? "└─ " : "├─ ";
String filePath = prefix + treeSymbol + files[i].getName();
if (files[i].isDirectory()) {
System.out.println("Directory: " + filePath);
listFilesAndFolders(files[i].getAbsolutePath(), prefix + INDENT);
} else {
System.out.println("File: " + filePath);
}
}
}
}
public static void main(String[] args) {
String currentDirectory = System.getProperty("user.dir");
System.out.println("Current Directory: " + currentDirectory);
System.out.println("Files and Folders in Current Directory (Alphabetical Order):");
listFilesAndFolders(currentDirectory, "");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment