首页 > 技术文章 > java包名转路径

javayida 2020-03-26 14:38 原文

Windows下的路径分隔符和Linux下的路径分隔符是不一样的,当直接使用绝对路径时,跨平台会暴出“No such file or diretory”的异常。

在windows中路径为:D:\usr
linux中:/usr/local

\ 和/ 但是又因为\在java中有转译的意思

Matcher.quoteReplacement方法就是将\转为普通的;如下:

在这里插入图片描述
javaFile类的部分静态字段,来源网络:
在这里插入图片描述
所以如果直接将.转为File.separator,在windows上会报错的:
在这里插入图片描述
路径转换的正确写法:

packName.replaceAll("\\.",  Matcher.quoteReplacement(File.separator))

推荐阅读