码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-11-20 15:34:55      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:ring   转换   art   exception   转义字符   color   oid   illegal   print   

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为"",处理时会被当作转义字符,后面看这个字符后一位,如果没有任何内容,就报标题中的异常了。

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

标签:ring   转换   art   exception   转义字符   color   oid   illegal   print   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!