一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - JAVA獲取文件絕對路徑的方法

JAVA獲取文件絕對路徑的方法

2019-12-09 14:38Benjamin_whx JAVA教程

這篇文章主要介紹了JAVA獲取文件絕對路徑的方法,涉及針對文件路徑的操作技巧,需要的朋友可以參考下

本文實例講述了JAVA獲取文件絕對路徑的方法。分享給大家供大家參考。具體實現方法如下:

 

復制代碼代碼如下:
/**
* 獲取一個類的class文件所在的絕對路徑。 這個類可以是JDK自身的類,也可以是用戶自定義的類,或者是第三方開發包里的類。
* 只要是在本程序中可以被加載的類,都可以定位到它的class文件的絕對路徑。
*
* @param cls
*            一個對象的Class屬性
* @return 這個類的class文件位置的絕對路徑。 如果沒有這個類的定義,則返回null。
*/
private String getPathFromClass(Class cls) throws IOException {
     String path = null;
     if (cls == null) {
       throw new NullPointerException();
     }
     URL url = getClassLocationURL(cls);
     if (url != null) {
       path = url.getPath();
       if ("jar".equalsIgnoreCase(url.getProtocol())) {
         try {
           path = new URL(path).getPath();
         }
         catch (MalformedURLException e) {
         }
         int location = path.indexOf("!/");
         if (location != -1) {
           path = path.substring(0, location);
         }
       }
       File file = new File(path.replaceAll("%20"," "));
       path = file.getCanonicalPath();
     }
     return path;
   }
   /**
    * 獲取類的class文件位置的URL。這個方法是本類最基礎的方法,供其它方法調用。
    */
   private URL getClassLocationURL(final Class cls) {
     if (cls == null) {
       throw new IllegalArgumentException("class that input is null");
     }
     URL result = null;
     final String clsAsResource = cls.getName().replace('.', '/').concat(".class");
     final ProtectionDomain pd = cls.getProtectionDomain();
     if (pd != null) {
       final CodeSource cs = pd.getCodeSource();
       if (cs != null) {
         result = cs.getLocation();
       }
       if (result != null) {
         if ("file".equals(result.getProtocol())) {
           try {
             if (result.toExternalForm().endsWith(".jar")|| result.toExternalForm().endsWith(".zip")) {
               result = new URL("jar:".concat(result.toExternalForm()).concat("!/").concat(clsAsResource));
             }
             else if (new File(result.getFile()).isDirectory()) {
               result = new URL(result, clsAsResource);
             }
           }
           catch (MalformedURLException ignore) {
           }
         }
       }
     }
     if (result == null) {
       final ClassLoader clsLoader = cls.getClassLoader();
       result = clsLoader != null ? clsLoader.getResource(clsAsResource): ClassLoader.getSystemResource(clsAsResource);
     }
     return result;
}

 

希望本文所述對大家的Java程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色图18p | 日本欧美大码a在线视频播放 | 久久棋牌评测 | 欧美伊人久久久久久久久影院 | 无人在线视频高清免费观看动漫 | 99热精品久久 | 国产精品久久现线拍久青草 | 牛牛色婷婷在线视频播放 | 国内精品91东航翘臀女神在线 | 国内精品哆啪啪 | 国产精品自产拍在线观看2019 | 黑人巨大精品战中国美女 | 青青草亚洲 | 色婷婷婷婷 | 性印度freehd | 四虎精品成人免费影视 | 亚洲激情一区 | 亚洲成年人专区 | 亚洲v日韩v欧美在线观看 | 日本高清在线不卡 | www.九九热| 免费特黄一级欧美大片在线看 | 91四虎国自产在线播放线 | 国产精品区一区二区免费 | 男人懂得网站 | 人体做爰aaaa免费 | 精品国产成人高清在线 | 手机在线观看网站免费视频 | 九九精品国产兔费观看久久 | 国产美女亚洲精品久久久久久 | 青草青草视频2免费观看 | 欧美高清乌克兰精品另类 | 1024香蕉视频 | 被黑人日 | 女人扒开下面让男人桶爽视频 | 11 13加污女qq看他下面 | 亚洲欧洲日产国码 最新 | 欧美精品一国产成人性影视 | 日韩亚洲欧美综合一区二区三区 | 91香蕉视频在线 | 成人依依网 |