0

I've got an image folder in my resources folder and there are pictures stored.

Now I want an Array in my Java Class who has stored the names of each Picture in the folder. Later I want to test if a picture name equals a specific word, but that can I handle by myself. I just dont know, how to make the 'PictureName' Array.

I am using Spring boot with Java.

4 Answers4

0

It can be done easily:

File folder = new File("your_path");
File[] listOfFiles = folder.listFiles();

        for (File file : listOfFiles) {
        if(file.isFile()){
            System.out.println("File Name:" + file.getName());
        }
    }
0
File folder = new File("C:\\path\\to\\your\\folder\\");
List<File> files = Arrays.asList(folder.listFiles());

Insert the path to the folder that contains all your images in the first line. In files List you will find the list of all the images in your folder in objects of type File.

Every File object has the method getName() that will return the name of the image.

amicoderozer
  • 2,046
  • 6
  • 28
  • 44
0

Try somethings like this(Check if f is file( because it can be dictionary):

File folder = new File("yourPathTofolder");
        File[] listOfFiles = folder.listFiles();
        for (File f: listOfFiles) {
            if (f.isFile()) {
                System.out.println("File " + f.getName());
            } 
        }
0

You can use below code:

  1. First provide your folder path, that will contain all the images.

File folder = new File("your/path");

  1. Store all files in File type array variable.

File[] listOfFiles = folder.listFiles();

  1. Iterate over variable to get filename(s)

    for (int i = 0; i < listOfFiles.length; i++) {
      if (listOfFiles[i].isFile()) {
        System.out.println("File " + listOfFiles[i].getName());
      } else  {
        System.out.println("Not a File..");
      }
    }
    



    Full code

    File folder = new File("your/path"); File[] listOfFiles = folder.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { System.out.println("File " + listOfFiles[i].getName()); } else { System.out.println("Not a File.."); } }


Hope that helps.

mayank bisht
  • 784
  • 9
  • 19