首页 > 解决方案 > 如何从随机文件夹中获取随机文件

问题描述

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

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

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

我发现这篇文章说我们可以通过这种方式从文件夹中选择一个随机文件:

File[] files = dir.listFiles();
Random rand = new Random();
File file = files[rand.nextInt(files.length)];

这对我来说听起来清晰而容易。但是,由于我所有的文件都有“简单编号的名称”,我想知道是否有办法在不列出所有文件的情况下做到这一点。获取目录中的文件总数对我来说应该足够了,因为它们的名称只是带有“。”的数字。

其次,这解决了选择随机文件的问题,而不是选择随机目录(并得到他的名字)的问题。

编辑: 我还发现这篇文章说一个“好的解决方案”(快速)来计算目录中的文件或目录的数量是:

File hugeDir = new File("/tmp/huge-dir");
int numberFiles = hugeDir.list().length;

我完全不知道我们可以创建一个带有目录的 File 对象。对我来说似乎很奇怪。但是,我想它可以解决我的两个问题:选择一个随机目录并获取他的名字,我这样做:

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

第一个功能运行良好;但是,在第二个中,目录名称列表为空。由于它与以前的代码相同,我不明白为什么。

标签: javarandomdirectory

解决方案


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

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

.

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

推荐阅读