Is there a way I could use some functions for setting the location(directory) of my files on the fly?
Every time I use to hardcode file location like this
String folder = "/Users/...../Desktop/sample";
File dir = new File(folder);
File[] files = dir.listFiles();
is there any method where I can avoid hardcoding? The location I’m using isn’t my current directory.
Suggestions or help in this regard appreciated. 🙂
As I see it some rather simple ways are feasible, but might not exactly be what you want:
Navigate from the classpath
or
Use a property file containing an entry for the path
where in your property file you would have a line like
Use environment variables
Where you would store the path in the environment variables (if applicable for your OS).
Give the path as a parameter
Well, that one is pretty straightforward… Just access the string array in main…
Alternatively I could think that it is possible to use the Java file system commands to manually traverse a directory structure and search for something in it. I would however not advise to do this, as this is not what they’re made for. If at all I guess I’d fall back on some 3rd party API.