Java Folder Read getFiles(String dirName, int number)

Here you can find the source of getFiles(String dirName, int number)

Description

get Files

License

Open Source License

Declaration

public static List<File> getFiles(String dirName, int number) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2012 Nikita Zhiltsov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html/*from  ww  w .  j  a v a2s. com*/
 * 
 * Contributors:
 *     Nikita Zhiltsov - initial API and implementation
 *     Azat Khasanshin - implementation
 ******************************************************************************/

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Main {
    public static List<File> getFiles(String dirName, int number) {
        File dir = new File(dirName);
        File[] files = dir.listFiles();
        List<File> selectedFiles = new ArrayList<File>();
        Random random = new Random();
        int i = 0;
        while (i < number) {
            int randomIndex = random.nextInt(files.length);
            File randomFile = files[randomIndex];
            if (!selectedFiles.contains(randomFile)) {
                selectedFiles.add(randomFile);
                i++;
            }
        }
        return selectedFiles;
    }
}

Related

  1. getFiles(String _path)
  2. getFiles(String dir)
  3. getFiles(String dir)
  4. getFiles(String dir)
  5. getFiles(String directoryPath)
  6. getFiles(String folderName, String prefix)
  7. getFiles(String parent)
  8. getFiles(String path)
  9. getFiles(String path)