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

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

posted @ 2019-11-20 15:02  韦邦杠  阅读(227)  评论(0编辑  收藏  举报