title: How to get a random file from a random folder
body: <p><strong>This is not a duplicate of "Counting the number of files in a directory using Java", since the main question here is "How to get a random file from a random folder". Counting the number of files was just an optional question for improvement.</strong></p>
  
  <p>I have a folder "testfiles" in my project directory, which contains a lot of others folders. Those folders all have names like "alice-g" (firstname and then first letter from lastname). Each of those folders contains some other folders, not always the same number. And in each of those last folders, i got a bunch of files, all named with number ("1.", "2.", "3." and so on). </p>
  
  <p>What i want to do is going into this "testfiles" directory, randomly select a folder, get his name, then randomly select another folder in it and finally select a random file in this folder (and get his name).</p>
  
  <p>I have found <a href="https://stackoverflow.com/questions/39629645/pick-a-random-file-from-a-directory-in-java">this post</a> which say we can pick a random file from a folder this way: </p>
  
  <pre><code>File[] files = dir.listFiles();
  Random rand = new Random();
  File file = files[rand.nextInt(files.length)];
  </code></pre>
  
  <p>This sound clear and easy to me. However, since all my files have "easy numbered names", i wonder if there is a way to do this without making a list of all files. Getting the total number of files in the directory should be enough to me, since their names are just number with a "."</p>
  
  <p>Second, this solve the problem of selecting a random file, but not of selecting a random directory (and get his name).</p>
  
  <p><strong>EDIT:</strong> 
  I also found <a href="https://stackoverflow.com/questions/39715607/by-java-how-to-count-file-numbers-in-a-directory-without-list">this post</a> which says that a "good solution" (fast) to count the number of files OR directories in a directory is: </p>
  
  <pre><code>File hugeDir = new File("/tmp/huge-dir");
  int numberFiles = hugeDir.list().length;
  </code></pre>
  
  <p>I totally didn't know that we could create a File object with a directory. Seems weird to me. However, i guess it can solve both of my question: to select a random directory and get his name, i do: </p>
  
  <pre><code>//Get a random client name according to directories names:
  private static String getRandomClient() {
      File testFiles = new File("testfiles");  //directory where all my testfiles are (in other directories)
      Random rand = new Random();
      String[] dirNames = testFiles.list();   //making a list() seems to be faster than making a listFiles().
      File randomNamedDirectory = new File(dirNames[rand.nextInt(dirNames.length)]);
      String randomDirName = randomNamedDirectory.getName();
      return randomDirName;
  }
  </code></pre>
  
  <p>Then i want to get a random file from a random directory from this client Directory: </p>
  
  <pre><code>//Get a random File Path from the client Directory:
  private static String getRandomFilePath(String clientDirectory) {
      File clientDir = new File("testfiles\\"+clientDirectory);   //client Directory.
      System.out.println(clientDir);
      Random rand = new Random();
      String[] dirNames = clientDir.list();  //This return null!?
      System.out.println(dirNames);
      File randomDirectory = new File(dirNames[rand.nextInt(dirNames.length)]);
      int numberFiles = randomDirectory.list().length;
      String randomFile = rand.nextInt(numberFiles) + ".";  //All files are named with their number and a .
      String filePath = clientDir + "\\" + randomDirectory +"\\" + randomFile;
      return filePath;
  }
  </code></pre>
  
  <p>The first function works well; however, in the second one, the list of Directories Names is null. Since it's just the same code as before, i don't understand why.</p>