Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert URL to AbsolutePath

Tags:

Is there any easy way to convert a URL that contains to two-byte characters into an absolute path?

The reason I ask is I am trying to find resources like this:

URL url=getClass().getResources("/getresources/test.txt"); String path=url.toString(); File f=new File(path); 

The program can't find the file. I know the path contain '%20' for all spaces which I could convert but my real problem is I'm using a japanese OS and when the program jar file is in a directory with japanese text (for example デスクトップ) I get the URL-encoding of the directory name, like this:

%e3%83%87%e3%82%b9%e3%82%af%e3%83%88%e3%83%83%e3%83%97

I think I could get the UTF-8 byte codes and convert this into the proper characters to find the file, but I'm wondering if there is an easier way to do this. Any help would be greatly appreciated.

nt

like image 441
nite Avatar asked Sep 02 '10 22:09

nite


1 Answers

URL url = getClass().getResource("/getresources/test.txt"); File f = new File(url.toURI()); 
like image 184
Noel Ang Avatar answered Sep 21 '22 04:09

Noel Ang