Load all files from resource folder

I was looking for a way to load all files in side my resource folder with out having to write code for each file so i came up with this and it works like this i write all files in that folder to text file and use that text file + this method to load all files so i dont have to write code for each file ::slight_smile:

package com.game.task;

import java.awt.Image;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Scanner;

import javax.swing.ImageIcon;

public class imagesTest {

public LinkedList<Image> storeImages = new LinkedList<Image>();
private LinkedList<String> storeStrings = new LinkedList<String>();

public imagesTest() {
    load();
    start();
}

public void readimage(String f) {


    Image temp = new ImageIcon(this.getClass().getResource("/res/"+f)).getImage();//looks for it
    storeImages.add(temp);//put it in a linked-List(Image)

}

public void start(){
    for(int index = 0; index < storeStrings.size(); index++){
        readimage(storeStrings.get(index));//calling the methode to look in resource folder
    }
}
//reads the text file and puts the out put in a linked-list(String)
private void load() {
    File file = new File("Data/startUp.txt");
    try {
        Scanner read = new Scanner(file);
        while (read.hasNextLine()) {
            storeStrings.add(read.nextLine());
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

public static void main(String[] arsg) {
    imagesTest t = new imagesTest();
}
}

have you added the res folder to your project path?

right click project
go to properties
java build path
Libraries
Add Class Folder

then add the res folder

yes i did that’s why i made this methode :stuck_out_tongue:

Why do you use LinkedList?

because i prefer linked list instead of doing Image[] or String[]

Why not ArrayList?

This page should answer your question: http://stackoverflow.com/questions/1844688/read-all-files-in-a-folder