java包名转路径

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))
世界上所有的不公平都是由于当事人能力不足造成的.
原文地址:https://www.cnblogs.com/javayida/p/13346770.html