Java 如何从随机文件夹中获取随机文件

Java 如何从随机文件夹中获取随机文件,java,random,directory,Java,Random,Directory,这不是“使用Java计算目录中的文件数”的重复,因为这里的主要问题是“如何从随机文件夹中获取随机文件”。计算文件数只是需要改进的可选问题。 我的项目目录中有一个文件夹“testfiles”,其中包含许多其他文件夹。这些文件夹都有“alice-g”(名字和lastname的第一个字母)这样的名称。这些文件夹中的每一个都包含一些其他文件夹,但编号并不总是相同的。在最后的每一个文件夹中,我都得到了一堆文件,都用数字(“1”,“2”,“3.”等等)命名 我想做的是进入这个“testfiles”目录,随机

这不是“使用Java计算目录中的文件数”的重复,因为这里的主要问题是“如何从随机文件夹中获取随机文件”。计算文件数只是需要改进的可选问题。

我的项目目录中有一个文件夹“testfiles”,其中包含许多其他文件夹。这些文件夹都有“alice-g”(名字和lastname的第一个字母)这样的名称。这些文件夹中的每一个都包含一些其他文件夹,但编号并不总是相同的。在最后的每一个文件夹中,我都得到了一堆文件,都用数字(“1”,“2”,“3.”等等)命名

我想做的是进入这个“testfiles”目录,随机选择一个文件夹,获取他的名字,然后随机选择其中的另一个文件夹,最后在这个文件夹中选择一个随机文件(获取他的名字)

我发现可以这样从文件夹中随机选取一个文件:

File[] files = dir.listFiles();
Random rand = new Random();
File file = files[rand.nextInt(files.length)];
对我来说,这听起来清晰而容易。然而,由于我所有的文件都有“容易编号的名称”,我想知道是否有一种方法可以做到这一点,而不必列出所有的文件。获取目录中的文件总数对我来说应该足够了,因为它们的名称只是带“.”的数字

其次,这解决了选择一个随机文件的问题,而不是选择一个随机目录(并获取他的名字)

编辑: 我还发现,计算目录中文件或目录数量的“好办法”(fast)是:

File hugeDir = new File("/tmp/huge-dir");
int numberFiles = hugeDir.list().length;
我完全不知道我们可以创建一个带有目录的文件对象。我觉得很奇怪。然而,我想它可以解决我的两个问题:选择一个随机目录并获取他的名字,我会:

//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;
}
然后我想从这个客户机目录的随机目录中获取一个随机文件:

//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;
}

第一个功能运行良好;但是,在第二个目录中,目录名列表为空。因为代码和以前一样,我不明白为什么。

所以,经过一些研究,我发现: 1.似乎并没有比列出文件更好的解决方案了。但是,使用list()而不是listFiles()似乎要快一点(参见问题编辑部分引用的帖子)

  • 我在问题中给出的“部分解决方案”中有一些愚蠢的错误(例如,使用\而不是/)。以下是一个“最小且完整”的解决方案:

  • 你说文件有“容易编号的名称”。你能展示一个包含一些文件夹和文件的示例目录结构吗?这个问题可能是关于“获取目录中的文件数(不列出所有文件)”或“选择一个随机目录(并获取其名称)”?@M.Taylor是的,这基本上反映了我对这个主题的看法。如果性能是一个问题(这里绝对没有证据证明这是一个问题,因为没有执行任何测试),那么让操作系统为您完成这项工作。您发现的示例非常好。你想用不同的方式做的原因是什么?如果是优化,你现在得到了什么样的性能数据?
    import java.io.File;
    public class main {
    
        public static void main(String[] args) throws Exception{
    
            String client = getRandomClient(); 
            String filePath = getRandomFilePath(client);
            File file = new File(filePath);        
        }
    
        //Get a random client name according to directories names:
        private static String getRandomClient() {
            File testFiles = new File("maildir");  //directory where all my testfiles are (in other directories)
            Random rand = new Random();
            String[] dirNames = testFiles.list();
            File randomNamedDirectory = new File(dirNames[rand.nextInt(dirNames.length)]);
            String randomDirName = randomNamedDirectory.getName();
            return randomDirName;
        }
    
        //Get a random File Path from the client Directory:
        private static String getRandomFilePath(String clientDirectory) {
            File clientDir = new File("maildir/"+clientDirectory);      //client Directory.
            System.out.println(clientDir);
            Random rand = new Random();
            String[] dirNames = clientDir.list();
            File randomDirectory = new File(clientDir +"/" + 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 = randomDirectory + "/" + randomFile;
            return filePath;
        }