Results 1 to 4 of 4
  1. #1
    killerwake is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default File List shuffle

    greetings to this community.

    i'm new on java and i'm trying to create a File List, and i used some scripts on google, but the problem is how i can shuffle the filelist????? here is the code:

    package com.wowza.wms.example;

    import java.io.File;

    public class FolderTraversar {

    private File fileObject;

    public FolderTraversar(String fileObjectname)
    {

    fileObject = new File(fileObjectname);


    }

    public void traverse()
    {
    recursiveTraversal(fileObject);
    }

    public void recursiveTraversal(File fileObject){

    if (fileObject.isDirectory()){
    //System.out.println(indent + fileObject.getName());
    File allFiles[] = fileObject.listFiles();
    for(File aFile : allFiles){
    recursiveTraversal(aFile);
    }
    }else if (fileObject.isFile()){
    //System.out.println(indent + " " + fileObject.getName());
    System.out.println(fileObject.getAbsoluteFile());
    }
    }

    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    the problem is how i can shuffle the filelist?

    What file list?

    In the code you obtain an array allFiles of the children of the File currently being visited. Is this what you are trying to shuffle? If so, why?

    -------------------------

    When posting code use the code tags: you put [code] at the start of the code and [/code] at the end. That way the indenting is preserved and it is easier to read.

  3. #3
    killerwake is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    i'm trying to shuffle the files for a running playlist, and i don't want the same sort.

    yes it's what i want

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Use Arrays.asList() to obtain a list. Then use Collections.shuffle() on the list.

    Java Code:
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    
    public class ArrayShuffleEg {
    
        public static void main(String[] args) {
            String[] data = {"one", "two", "three", "four", "five"};
    		
            List<String> list = Arrays.asList(data);
            Collections.shuffle(list);
    		
            for(String str :list) {
                System.out.println(str);
            }
        }
    }

Similar Threads

  1. Deck Shuffle
    By Cragsterboy in forum New To Java
    Replies: 1
    Last Post: 12-13-2010, 05:23 PM
  2. Shuffle Group Method
    By xelo in forum New To Java
    Replies: 6
    Last Post: 01-05-2009, 08:41 PM
  3. 2D Array Random Shuffle
    By Nuluvius in forum New To Java
    Replies: 8
    Last Post: 11-23-2008, 07:15 PM
  4. How do I shuffle an arraylist?
    By frasifrasi in forum New To Java
    Replies: 2
    Last Post: 07-17-2008, 12:29 AM
  5. How to Shuffle a particular String
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-06-2008, 08:38 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •