I encountered a problem with getPath() recently.
my code looks something like this:
File path = new File(Main.class.getResource("/worlds/").getPath());
File[] files = path.listFiles();
The Problem now is, that if there is a space somewhere in the Path to the Main class, path.listFiles()
will return null
. If there is no Space, everything works fine.
if i print the path to the cmd, i see that every space is replaced by an %20
that is not the correct way to convert a URL to a File. try this instead:
new File(Main.class.getResource("/worlds/").toURI());
Don't do that. A resource URL returned by getResource() isn't necessarily a file on the file system, which is what File represents.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With