Skip to content

Instantly share code, notes, and snippets.

@raimonizard
Last active December 13, 2023 11:46
Show Gist options
  • Save raimonizard/68053bf1c1050caa40471bf6a0f9d5cc to your computer and use it in GitHub Desktop.
Save raimonizard/68053bf1c1050caa40471bf6a0f9d5cc to your computer and use it in GitHub Desktop.
Funció recursiva en Java per a explorar el contingut d'una carpeta
import java.io.File;

public class Carpetes {
    public static void main(String[] args) {
        File root = new File("Data");
        carpetes(root);
    }

    public static void carpetes(File f){
        if (f.isDirectory()){
            File[] arxius = null;
            arxius = f.listFiles();
            if (arxius != null && arxius.length > 0) {
                for (int i = 0; i < arxius.length; i++) {
                    System.out.println("La carpeta " + f.getName() + " té un arxiu anomenat: " + arxius[i]);
                    carpetes(arxius[i]);
                }
            }else{
                System.out.println("La carpeta: " + f.getName() + " està buida");
            }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment