Still just learning java and I have a bit of a newbie question. I'm working with arrays and trying to learn how to sort / shuffle them. I picked up a piece of old code that I had that generated a list of files in a directory and now I want to be able to randomize that list and print the file names in the new random order. I've got no problem creating an array of the file names, and the printing should be fine, but I'm struggling with the randomization part. The closest thing to what I want is shuffle(), but that's for a list not an array. I'll post the relevant code below and welcome any input.
shuffle() obviously isn't the answer, but I can't find anything like it for arrays.
Code:
class Fiel
{
String path="c:\\ren\\";
File theFile;
byte[] data;
public void genList() throws IOException
{
File dir = new File(path);
File[] list = dir.listFiles();
File targ = new File(path, "0.txt");
Collections.shuffle(list);
byte[] piped;
FileOutputStream fos = new FileOutputStream(targ);
for(int i=0;i<list.length;i++)
{
File current=list[i];
String fname = current.getName();
String topipe = new String(fname + "/nl");
piped = topipe.getBytes();
try
{
fos.write(piped);
}
catch(IOException e)
{
System.out.println("IOException " + e.getMessage());
e.printStackTrace();
}
}
fos.flush();
fos.close();
}
}
shuffle() obviously isn't the answer, but I can't find anything like it for arrays.