| | |
| | | |
| | | import java.io.File; |
| | | import java.net.URL; |
| | | import java.net.URLDecoder; |
| | | |
| | | public class PathUtil { |
| | | /** |
| | |
| | | */ |
| | | public static final String projectPath = initProjectPathAndDebug(); |
| | | |
| | | public static String getConfigFilePath(String cfgName) { |
| | | return String.format("%s%s%s", projectPath, getFileSeparator(), cfgName); |
| | | } |
| | | |
| | | /*** |
| | | * 获取项目根路径,无论是打包成jar文件。 |
| | | * 为了保证调试时获取项目路径,而不是bin路径,增加逻辑: 如果以bin目录接,则返回上一层目录 |
| | |
| | | * @return 例如:F:\eclipse\work\JavaFX\AddressApp\build\dist |
| | | */ |
| | | private static String initProjectPathAndDebug() { |
| | | |
| | | if (SystemUtil.isWindows()) { |
| | | System.out.println(".....isWindows...."); |
| | | } |
| | | if (SystemUtil.isMacOs()) { |
| | | System.out.println("....isMacOs....."); |
| | | } |
| | | if (SystemUtil.isLinux()) { |
| | | System.out.println("......isLinux..."); |
| | | } |
| | | |
| | | |
| | | URL url = PathUtil.class.getProtectionDomain().getCodeSource().getLocation(); |
| | | String filePath = null; |
| | | try { |
| | | filePath = java.net.URLDecoder.decode(url.getPath(), "utf-8"); |
| | | filePath = URLDecoder.decode(url.getPath(), "utf-8"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | |
| | | return null; |
| | | } |
| | | |
| | | public static String getFileSeparator() { |
| | | return File.separator; |
| | | } |
| | | |
| | | /*** |
| | | * 这个方法打包位jar文件就无法获取项目路径了。 |
| | | * @return |
| | |
| | | File file = new File(realPath); |
| | | realPath = file.getAbsolutePath();//去掉了最前面的斜杠/ |
| | | try { |
| | | realPath = java.net.URLDecoder.decode(realPath, "utf-8"); |
| | | realPath = URLDecoder.decode(realPath, "utf-8"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |