GetResource () of ClassLoader returns null when a single quote is in the path

I am trying to get ressource of SQL file in src / main / resources.

I managed to get the file with this:

String sqlFileName = "query.sql"; URL url = MyClass.class.getClassLoader().getResource(sqlFileName); 

But no, if there is one quote in sqlFileName

 String sqlFileName = "John query.sql"; URL url = MyClass.class.getClassLoader().getResource(sqlFileName); 

getResource returns null because it cannot find my resource when there is an apostrophe. I tried several methods to avoid my separate quote, such as \ 'and', and none of them worked.

I expect to get a url with getResource similar to this:

 C:\Projects\ProjectA\src\main\resources\queries\John query.sql 
+6
source share
2 answers

Did you try to escape with \\ '?

With one \ ', you create escaped' for String, but it is not passed to the URL parser. You want to avoid \ the \, so both and \ both are passed to the parser.

0
source

You can try to encode the file name,

URLEncoder.encode ("french'filename.txt", "UTF-8"));

It will return: french% 27filename.txt You can choose a different encoding if you need.

0
source

All Articles