0

I need to upload a Folder with sum x of .png files and save them into a hashmap. How can I achieve this. My thought was

    HashMap<Integer,Image> map = new HashMap<>();
    for (int i= map.size();map>0;map-- ){
        map.put(i, new Image(new FileInputStream("C:\\Users\\drg\\Documents\\image"+i+".png")));
    }

the problem is, in the beginning my HashMap contains 0 items, therefor it will always remain 0. So how can I add the .png files to my HashMap if I dont know how many .png files are in my folder.

Another problem is that I am using the FileInputStream and need to know the exact "name" of the .png file. How can I find out how many .png files there are and upload them into my HashMap without needing to know their exact filename?

drg
  • 3
  • 1
  • 2
    What are your exact requirements? Do you *need* to index them by the integer value? Or would any collection of images suffice? Assuming you are really loading from the file system, all you need to do here is list the contents of the appropriate folder. – James_D Nov 24 '20 at 13:34
  • this might be useful: https://stackoverflow.com/questions/4852531/find-files-in-a-folder-using-java – RIVERMAN2010 Nov 24 '20 at 13:51

2 Answers2

1

All you need to do is list the contents of the folder and find the matching file names. E.g. using the java.nio API and putting the images into a list:

Path folder = FileSystems.getDefault().getPath("Users", "drg", "Documents");
List<Image> images = Files.list(folder)
    .filter(path -> path.getFileName().toString().matches("image\\d+\\.png"))
    .map(Path::toUri)
    .map(URI::toString)
    .map(Image::new)
    .collect(Collectors.toList());

Or using the old java.io API:

File folder = new File("C:/Users/drg/Documents");
File[] imageFiles = folder.listFiles(file -> file.getName().matches("image\\d+\\.png"));
List<Image> images = new ArrayList<>();
for (File file : imageFiles) {
    images.add(new Image(file.toURI().toString()));
}

If you need to index them by the integer, it's reasonably easy to extract that integer value when you have the filename.

James_D
  • 201,275
  • 16
  • 291
  • 322
0

If it helps, you can help use a FileChooser to pick the files manually (like with a GUI) and you can load them into a List and then it might be easier to go from a list to a map?

```
private void uploadPhoto() {
    FileChooser fileChooser = new FileChooser();
    //Sets file extension type filters (Pictures)
    fileChooser.getExtensionFilters().addAll
            (new FileChooser.ExtensionFilter("Picture Files", "*.jpg", "*.png"));
    
    //Setting initial directory (finds current system user)
    File initialDirectory;
    String user = System.getProperty("user.name"); //platform independent
    List<File> selectedFiles;
    try {
        initialDirectory = new File("C:\\Users\\" + user + "\\Pictures" );
        fileChooser.setInitialDirectory(initialDirectory);  
        selectedFiles = fileChooser.showOpenMultipleDialog(Main.stage);
    } catch(Exception e) {
        initialDirectory = new File("C:\\Users\\" + user);
        fileChooser.setInitialDirectory(initialDirectory);
        selectedFiles = fileChooser.showOpenMultipleDialog(Main.stage);
    }
```