Mic*_*ski 6 java eclipse encoding utf-8
我想得到一个文件的绝对路径,以便我可以进一步使用它来找到这个文件.我这样做的方式如下:
File file = new File(Swagger2MarkupConverterTest.class.getResource(
"/json/swagger.json").getFile());
String tempPath = file.getAbsolutePath();
String path = tempPath.replace("\\", "\\\\");
Run Code Online (Sandbox Code Playgroud)
路径irl看起来像这样:
C:\\Users\\Micha? Szyd?owski\\workspace2\\swagger2markup\\bin\\json\\swagger.json
Run Code Online (Sandbox Code Playgroud)
但是,因为它包含波兰字符和空格,所以我得到的getAbsolutPath
是:
C:\\Users\\Micha%c5%82%20Szyd%c5%82owski\\workspace2\\swagger2markup\\bin\\json\\swagger.json
Run Code Online (Sandbox Code Playgroud)
我怎样才能以正确的方式做到这一点?这是有问题的,因为使用此路径,它无法找到该文件(说它不存在).
URL.getFile
您正在使用的调用返回根据URL编码规则编码的URL的文件部分.在将字符串URLDecoder
赋予之前,您需要使用它来解码File
:
String path = Swagger2MarkupConverterTest.class.getResource(
"/json/swagger.json").getFile();
path = URLDecoder.decode(filePath, "UTF-8");
File file = new File(path);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5370 次 |
最近记录: |