How to iterate through the names of current directories in Java?

You can use the File class in Java to iterate over the names of files and folders in the current directory. Here is an example code:

import java.io.File;

public class Main {
    public static void main(String[] args) {
        File folder = new File(".");
        
        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            
            for (File file : files) {
                if (file.isDirectory()) {
                    System.out.println("Folder: " + file.getName());
                } else {
                    System.out.println("File: " + file.getName());
                }
            }
        }
    }
}

In the example above, we first create a File object to represent the current folder (using “.” to indicate the current directory). We then check if the folder is a directory, and if so, we obtain a list of files and subdirectories within the folder. Next, we iterate through this list and print out the corresponding names based on the file type (file or folder).

Please note that this is just a simple example, in actual applications more logic may be needed to handle file and folder operations.

bannerAds