0

something simple. It has bothered me since long that I had only one folder where I should put all textures of my project - just one! You can imagine that this can quickly become disorganized.

BufferedImage image = ImageIO.read(new File("./res/textures/" + fileName));

This little piece of code will allow me to get the respective file from the textures folder within my project, java can only find the 'fileName' String within this single folder.

Since I want to have multiple folders within the textures folder, I want to know if and how it is possible to tell java that it needs to check all folders within the 'textures' folder for any given 'fileName', has anyone a suggestion?

5
  • loop the sub folder, loop the files and search for it..., there is not automatic command if you don't know the exact path.. Commented Nov 15, 2015 at 22:26
  • It depends, you could make fileName contain the subfolder prefix /ground/stone.png for example. You could use either static final constants for each "group" or an enum, Textures.GROUND.load("stone.png") which could generate the path to the "ground" textures folder and suffix the image name Commented Nov 15, 2015 at 22:29
  • 1
    You could also use Paths to Walk a file tree - but this is VERY inefficient. It'd be better to pre-scan the directories and generate a Map of each texture by name Commented Nov 15, 2015 at 22:30
  • Or even better, generate a simple mapping file (key=value) at build time Commented Nov 15, 2015 at 22:40
  • use the commons-apache-io's fileutils, there's a file filter that can be used. See this Commented Nov 15, 2015 at 22:48

1 Answer 1

1

You want to put textures into subfolder because it's a mess having them all lumped together, but you want to re-introduce that mess in your code by referring to them only by name?

Your fileName component doesn't have to be just the filename, you can have subpaths there as well - which is what you'll want to do. It's simpler, faster and more organized than scanning through subfolders.

Sign up to request clarification or add additional context in comments.

1 Comment

oh I see now.. yes that makes sense so instead of just saying "stone.png" I can say something like "/directory1/directory2/stone.png".. Thanks, don't know why this didn't occur to me..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.