0

I'm writing a slideshow which allows the pictures it shows to be deleted, edited or ignored as they are displayed. It's working fine except for when the directory it's been pointed to contains subfolders. In this case I would like the code to search through the subfolders and also display the images within them. At the moment subfolders are ignored and a null pointer exception is thrown, though images in the main directory are read and displayed. This is the relevant section of the code:

    String files;
    File folder = new File(path);                                     //Loads folder
    File[] listOfFiles = folder.listFiles();                          //assigns filenames to array
    for (int i = 0; i < listOfFiles.length; i++) {

        //Some code here deals with graphics

        Random randomGenerator = new Random();                        //Initiates random generator
        int randomInt = randomGenerator.nextInt(listOfFiles.length);  //Limits max random to lenth of folder array
        files = listOfFiles[randomInt].getName();                     //Chooses a filename based on random number
        final String fileID = "file:///" + path + "\\" + files;       //creates path to display
        currentFile = path + "\\" + files;                            //Assigns filename to variable ready for deletion later
    }

I need to treat these more as discrete images rather than just printing out a list of them. I've excluded from the code here the snippet which deals with painting to the screen (g.drawImage).

If anybody suggests Apache Commons IO FileUtils, would you be kind enough to explain how I go about downloading, installing and then importing the correct libraries, as I've been having trouble achieving this (if it even works). I would prefer a solution which doesn't require non-standard libraries. I have read that it is do-able in Java 8 (which I'm using) but I've not been able to find any relevant examples.

I apologise if this turns out to be a duplicate question, but I've been unable to find a similar example that quite matches these requirements. Most other examples seem content simply to list the files, rather than perform an action with them.

Many thanks in advance.

Guy Stimpson
  • 87
  • 11
  • See [Recursively list files in Java](http://stackoverflow.com/questions/2056221/recursively-list-files-in-java) for many possible solutions – Paul Samsotha Jun 19 '14 at 09:45
  • I've seen that but the most highly voted answers tend to involve Apach FileUtils or other non-standard libraries and other answers list the contents of the main folder rather than going in to individual subfolders and reading the files contained there. That's why I've posted my specific example in the hope that someone will be kind enough to address my problem with something other than a blanket reply. – Guy Stimpson Jun 19 '14 at 10:07
  • Recursion (in the context of a directory search )is process by which you search the main directory, if its a file, grab it, if its a directory, recursively go through that. You may want to read up on recursion. It's important when dealing with files. That being said, most of the answers _do_ use standard library. There are answer using Java 8 and answers user lower APIs. But for the most part, I don't think they would've been upvoted if they didn't do the job. Why don't you show your attempt as using one of the shown methods. An provide an MCVE show what you've tried and the result. – Paul Samsotha Jun 19 '14 at 10:13
  • Other than that, any answer I give you will just be a repeat of one of _those answers. – Paul Samsotha Jun 19 '14 at 10:13
  • Sorry, could you please expand on 'MCVE'? I get the impression that's a Stack Overflow - specific acronym and I'm a bit of a nood (if you couldn't tell). – Guy Stimpson Jun 19 '14 at 10:17
  • It just stands for Minimal Complete Verfiable Example. [Here's the link](http://stackoverflow.com/help/mcve). Also, sorry for being lazy to read the whole question, but do you just want all the file a a directory and subdirectory and make those images? – Paul Samsotha Jun 19 '14 at 10:21
  • Also are these files going to be resources embedded to your jar, or are they system files? If the former, I strongly suggest you just put _all_ the images into a single directory. Trying to recursively walk a resource tree to get URLs is a bit more of daunting task and I wouldn't recommend it – Paul Samsotha Jun 19 '14 at 10:26
  • No problem, thanks for your patience. So I need all the files in all of the subdirectories to be painted by g.drawImage. I think my main difficulty is that the outputted imaged have to be randomised. The source folder will change depending on user input (well, the particular folder is held in a .txt file) which is dealt with elsewhere in the code. I realise that having multiple subdirectories will result in slow running, but this isn't much of a problem as each image is displayed for around 10 seconds so there is plenty of time for the code to catch up. – Guy Stimpson Jun 19 '14 at 10:26
  • I'm currently writing a second project in order to practice recursion, upon your recommendation :) – Guy Stimpson Jun 19 '14 at 10:30
  • @ peeskillet OK I'm getting somewhere. I can now list all the files in the appropriate directory and subdirectories. I would now like to place the resultant file paths (which I've just listed) into my original array (listOfFiles[]) so that I can continue as before with the original code intact. – Guy Stimpson Jun 19 '14 at 10:40
  • But I'm struggling with that now. Grrr! Why Java, why! – Guy Stimpson Jun 19 '14 at 10:41
  • I'm not getting what the problem is. Since you want to get the images, in the recursive process of obtaining the files, add create an image from the file (if is a file) and add it to a list of images. Can you explain in once sentence what you trying to achieve exactly? Maybe I can give you a simple example – Paul Samsotha Jun 19 '14 at 10:46
  • It's OK I'm basically there now. I have the list, which is excellent. Now I just need to come up with a way of randomising my list so that when they're painted, they're not always in the same order (boring). I think I should be able to get there now, you've been most helpful, thank you. I'll figure it out from here I think. – Guy Stimpson Jun 19 '14 at 10:49

0 Answers0