Java reset for loop so file can be read from the beggining in infinite loop

Marcin :

I have methods to read file and check for extension

//using FileChooser to select folder we want to get photos from
    static final File dir = new File("D:\\Pictures\\NatGeoPaper2020\\3\\");
    //array of supported extensions
    static final String[] EXTENSIONS = new String[]{"jpg", "jpeg", "png"};

    //filter to identify images by extensions
    static final FilenameFilter IMAGE_FILTER = new FilenameFilter() {
        @Override
        public boolean accept(final File dir, final String name) {
            for (final String ext : EXTENSIONS) {
                if (name.endsWith("." + ext)) {
                    return true;
                }
            }
            return (false);
        }
    };

In main I invoke those methods - basically read all images and show as slideshow to user.

if (dir.isDirectory()) {
            for (final File f : dir.listFiles(IMAGE_FILTER)) {
                BufferedImage img = null;
                try {
                    img = ImageIO.read(f);
                    SPI.INSTANCE.SystemParametersInfo(
                            new UINT_PTR(SPI.SPI_SETDESKWALLPAPER),
                            new UINT_PTR(0),
                            dir + "/" + f.getName(),
                            new UINT_PTR(SPI.SPIF_UPDATEINIFILE | SPI.SPIF_SENDWININICHANGE));
                    Thread.sleep(6000);
                } catch (final IOException e) {
                    throw new Error(e);
                }
            }
        }

Now my problem is, how can I "reset" my for loop in main to achieve infinite loop? I just want to change wallpaper every 6 seconds infinitely, withour rerunning a program.

Bryan :

Put a while(true) loop around it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=193627&siteId=1