I am getting this error when I try to open a file:
java.io.FileNotFoundException: D:\\Portable%20Programs\\Android%20Development\\workspace3\\XXX-desktop\\bin\\W
Try leaving out the %20, and use normal spaces instead. Also, you're using backslashes, in your code if you're using backslashes make sure you escape them first.
The preferred way to convert a file:
URL into an actual File
is this:
File file = new File(url.toURI());
This takes care of all checks and quoting/escaping.
Using getPath()
instead will leave these odd bits up to you.
You need to unescape the %20
to spaces. e.g.:
fileLocation = new String(
Main.class.getProtectionDomain().getCodeSource().getLocation().getPath())
.replaceAll("%20", " ");
Here is the solution for that , this will work only after JDK1.5 ,
try { f = new File("somePath".toURI().getPath()); } catch(Exception e) {}
The confirmed solution is quite old and even though it works for this particular case it is far more convenient to use URLDecoder, because %20 is only one encoded character, but in your path there can be whole lot of different encoded characters.
fileLocation = URLDecoder.decode(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath(), "UTF-8");