首页 > 其他 > 详细

获取当前项目路径+转换路径符号

时间:2019-11-20 15:39:26      阅读:91      评论:0      收藏:0      [点我收藏+]
String filePath = this.getClass().getResource("/").getPath();
filePath = filePath.replaceAll("\\/|"+Matcher.quoteReplacement("\\"),Matcher.quoteReplacement(File.separator));
System.out.println("filePath: " + filePath+"html" +File.separator+"56465");

replace File.separator出现异常:【java.lang.IllegalArgumentException: character to be escaped is missing】...

 public static void main(String[] args){
        String filePath = this.getClass().getResource("/").getPath();
        filePath = filePath.replaceAll("\\", File.separator);
        System.out.println(filePath);
}

原因在于,windows下File.separator为"",处理时会被当作转义字符,后面看这个字符后一位,如果没有任何内容,就报标题中的异常了。

获取当前项目路径+转换路径符号

原文:https://www.cnblogs.com/weibanggang/p/11897849.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!