GetPath () and spaces in Java

I recently ran into getPath () problem.

my code looks something like this:

File path = new File(Main.class.getResource("/worlds/").getPath()); File[] files = path.listFiles(); 

Now the problem is that if there is a space in the Path to Basic class, path.listFiles() will return null . If there is no Space, everything works fine.

if I print the path to cmd, I see that every space is replaced with %20

+7
source share
2 answers

Do not do this. A resource URL returned by getResource () is not necessarily a file in the file system, which means File .

+3
source

This is the wrong way to convert the url to file. try this instead:

 new File(Main.class.getResource("/worlds/").toURI()); 
+13
source

All Articles