Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8791391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:50:11+00:00 2026-06-13T22:50:11+00:00

I have a folder containing 100000 files, and need to get 1000 files from

  • 0

I have a folder containing 100000 files, and need to get 1000 files from this folder through random sampling. Are there any sample functions that I can use to sample from folder? In addition, how to copy the sampled files to another folder?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:50:12+00:00Added an answer on June 13, 2026 at 10:50 pm

    Random selection could follow along the following lines

    File files[] = new File("/path/to/files").listFiles();
    Map<Integer, File> selection = new HashMap<Integer, File>(1000);
    while (selection.size() < 1000) {
        int value = (int)Math.round(Math.random() * files.length);
        if (!selection.containsKey(value)) {
            selection.put(value, files[value]);
        }
    }
    for (File file : selection.values()) {
        System.out.println(file);
    }
    

    Essentially, you need to grab a list of the available files and the randomly pick through the list until you have enough of a sample. Check out java.io.File

    There are plenty of examples of file copying over the net (and SO). If you’re really stuck you could have a look the IO Trail or Apache Commons IO which I believe has a utility class capable of coping files

    UPDATED

    As suggested by Andrew, you could simply shuffle the file list and pull the first 1000 elements…

    File files[] = new File("/path/to/files").listFiles();
    List<File> selection = null;
    List<File> fileList = new ArrayList<File>(Arrays.asList(files));
    Collections.shuffle(fileList);
    selection = fileList.subList(0, Math.min(1000, fileList.size()));
    
    for (File file : selection) {
        System.out.println(file);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in my project i have a folder containing N number of files. I need
Is there any way, with the Eclipse IDE, to have a master folder containing
I have a php script that steps through a folder containing tab delimited files,
i want to download folder containing files like pdf,jpg,png etc from web services.I have
I have a folder containing .tcb and .tch files. I need to know what
I have folder home/admin. In this folder there is index.php. When i access to
I have a folder containing hundreds of TTL (TeraTermLanguage) files. Now I wanted indent
I have folder containing several files and sub-folders such as the following: -/folder -/subfolder
Is there any way in Spotlight to have a folder open in a Terminal
Plone version: 4.1.4 I have one folder containing 50 files. Author info can be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.