0

I'm trying to scan a folder of images and add them to an arraylist. I want to combine a scanner and random variable to randomly select one of the images from the array list and attach it to to a private ImageIcon icon. "FemaleFaces" is the directory with the images. So far, here is my code and thanks for any help!

private ImageIcon iconex; 

        File f = new File("FemaleFaces");
        Scan = new Scanner(f);
        ArrayList<ImageIcon> files = new ArrayList<ImageIcon>();
        while(Scan.hasNext())
        {
            files.add(new ImageIcon(Scan.next()));
        }
        Scan.close();

        int Ffindex = new Random().nextInt(files.size());
        iconex = files.get(Ffindex);

Exception in thread "main" java.io.FileNotFoundException: FemaleFaces (Access is denied) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(Unknown Source) at java.util.Scanner.(Unknown Source) at Human.(Human.java:66) at testerobjects.main(testerobjects.java:19)

4

1 回答 1

1

Scan.next()返回一个String。您需要ImageIcon从值构造 a 。

类似于...的东西

files.add(new ImageIcon(Scan.next()));
于 2013-05-28T02:28:43.413 回答