最开始我是用能够这种方式来获取当前 jar 包的文件的:
但是如果路径中包含中文,就会变成带“%xx%xx”格式的字符串。所以应该这样写才对:
private static String LOCATION = [MainClass].class.getProtectionDomain()
.getCodeSource().getLocation().getFile();
.getCodeSource().getLocation().getFile();
但是如果路径中包含中文,就会变成带“%xx%xx”格式的字符串。所以应该这样写才对:
private static String LOCATION;
static {
try {
LOCATION = URLDecoder.decode([MainClass].class.getProtectionDomain()
.getCodeSource().getLocation().getFile(), “UTF-8″);
} catch (UnsupportedEncodingException e) {
LOCATION = “”;
}
}
static {
try {
LOCATION = URLDecoder.decode([MainClass].class.getProtectionDomain()
.getCodeSource().getLocation().getFile(), “UTF-8″);
} catch (UnsupportedEncodingException e) {
LOCATION = “”;
}
}
本文介绍了一种处理Java程序中中文路径的方法。通过使用URLDecoder类的decode方法,并指定UTF-8编码方式,可以正确解析包含中文字符的路径。

被折叠的 条评论
为什么被折叠?



