繁体   English   中英

随机文件访问Java

[英]Random File Access Java

我知道有一种方法可以从目录中选择一个随机文件,但是我不知道它是如何用Java编码的。 我有伪代码。 我要问的是我能否朝着正确的方向发展。 我的伪代码如下:

dir = "directory";
String[] files = dir.listfiles();
String next = rand.nextInt(files.length);
Image img = next;

之所以要这样做,是因为我有一长串想要整理的图像。

您的伪代码看起来不错,您可以递归获取所有名称,将名称存储在ArrayList中,然后从ArrayList中随机检索名称,如下所示:

static ArrayList<String> files = new ArrayList<String>();

public static void main(String[] args) {
    File dir = new File(".");
    getFileNames(dir);
    Random rand = new Random();
    String next = files.get(rand.nextInt(files.size()));
}

private static void getFileNames(File curDir) {
    File[] filesList = curDir.listFiles();
    for (File f : filesList) {
        if (f.isDirectory())
            getFileNames(f);
        if (f.isFile()) {
            files.add(f.getName());
        }
    }
}

您似乎在正确的轨道上。 唯一的问题是listFiles()返回File[]而不是String[]

也许尝试这样的事情

File file = new File(filename);
File[] files = new File[0];     // initialize
if (file.isDirectory()){
    files = file.listFiles();   // populate
}
int fileIndex = new Random().nextInt(files.length);      // get random index
Image img = new ImageIcon(files[fileIndex]).getImage();  // create image

尽管上述方法可能有效,但建议对嵌入式资源而不是文件使用URL。 像这样

String[] filenames = file.list();    // list returns String
int fileIndex = new Random().nextInt(filenames.length); 
Image img = null;
java.net.URL url = MyClass.class.getResource(filenames[fileIndex]);
if (url != null){
    img = new ImageIcon(url).getImage();
} else {
    img = null;
}

使用class.gerResource() 该文件将在类文件的位置中搜索。 您也可以稍微更改路径,例如,如果您想要这样的文件结构

ProjectRoot
          bin
             MyClass.class
             images
                  image1.png
                  image2.png
          src

然后您可以使用此代码

java.net.URL url = MyClass.class.getResource("images/" + filenames[fileIndex]);

这是我将如何实现您的伪代码

private static final Random random = new Random(0x20131224 ^ // A seed value
      System.currentTimeMillis());                           // and more seed value(s).
public static File getRandomFile(String filePath) {
  File f = new File(filePath);                               // Do we have a directory?
  if (f == null || ! f.isDirectory()) {
    return f;
  }
  File[] files = f.listFiles();
  List<File> al = new ArrayList<File>();
  for (File file : files) {
    if (file != null && file.isFile() && file.canRead()) {   // Make sure it's a file.
      al.add(file);
    }
  }
  return al.get(random.nextInt(al.size()));                  // Get a random file.
}
File filedir=new File("C:\\Users\\ramaraju\\Desktop\\japan02-12\\");
File[] files=filedir.listFiles();

Random generator = new Random();
int Low = 0;
int High = files.length;
int R = generator.nextInt(High-Low) + Low;
System.out.println(R);
for (int i = 0; i < files.length; i++) {
    if(i==R)
    {
        System.out.println(files[i].getName());
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM