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

评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!