漂在爪洼島上

          Java is my life,but not all!
          posts - 3, comments - 6, trackbacks - 0, articles - 9
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          在java application中獲取當前工程/文件地址

          Posted on 2009-06-30 10:27 周競先 閱讀(1393) 評論(0)  編輯  收藏 所屬分類: J2SE
          1         String projectURL = System.getProperty("user.dir") ;
          2         System.out.println("工程所在的路徑地址:"+projectURL);
          3 

          輸出結果:
          1 工程所在的路徑地址:D:\J2EE\MyEclipse6.0_workspace\mobilenet2.0


          簡單說明:
          這個JAVA APPLICATION文件是在mobilenet2.0的工程里面,故打印出來的是工程所在路徑地址

          剛去csdn的博客看了一下,發現原來已經總結過一次,現在轉過來
           1 
           2
           31 、利用 System.getProperty() 函數獲取當前路徑: 
           4
           5System.out.println(System.getProperty("user.dir"));//user.dir 指定了當前的路徑 
           6
           7  
           8
           92 、使用 File 提供的函數獲取當前路徑: 
          10
          11File directory = new File("");// 設定為當前文件夾 
          12
          13try
          14
          15    System.out.println(directory.getCanonicalPath());// 獲取標準的路徑 
          16
          17    System.out.println(directory.getAbsolutePath());// 獲取絕對路徑 
          18
          19}
          catch(Exceptin e){} 
          20
          21  
          22
          23File.getCanonicalPath() 和 File.getAbsolutePath() 大約只是對于 new File(".") 和 new File("..") 兩種路徑有所區別。 
          24
          25  
          26
          27# 對于 getCanonicalPath() 函數,“ ." 就表示當前的文件夾,而” .. “則表示當前文件夾的上一級文件夾 
          28
          29# 對于 getAbsolutePath() 函數,則不管” . ”、“ .. ”,返回當前的路徑加上你在 new File() 時設定的路徑 
          30
          31# 至于 getPath() 函數,得到的只是你在 new File() 時設定的路徑 
          32
          33  
          34
          35比如當前的路徑為 C:\test : 
          36
          37File directory = new File("abc"); 
          38
          39directory.getCanonicalPath(); // 得到的是 C:\test\abc 
          40
          41directory.getAbsolutePath();    // 得到的是 C:\test\abc 
          42
          43direcotry.getPath();                    // 得到的是 abc 
          44
          45  
          46
          47File directory = new File("."); 
          48
          49directory.getCanonicalPath(); // 得到的是 C:\test 
          50
          51directory.getAbsolutePath();    // 得到的是 C:\test\. 
          52
          53direcotry.getPath();                    // 得到的是 . 
          54
          55  
          56
          57File directory = new File(".."); 
          58
          59directory.getCanonicalPath(); // 得到的是 C:\ 
          60
          61directory.getAbsolutePath();    // 得到的是 C:\test\.. 
          62
          63direcotry.getPath();                    // 得到的是 .. 
          64
          65 
          66
          67關于System.getProperty方法的參數見 
          68
          69
          70 
          71


          Life,simple and happy!


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 临朐县| 天台县| 加查县| 大安市| 清徐县| 通江县| 衡山县| 烟台市| 营山县| 白朗县| 清徐县| 三河市| 大冶市| 小金县| 崇州市| 察隅县| 苗栗市| 马公市| 莫力| 漾濞| 攀枝花市| 宁国市| 海门市| 五常市| 沧州市| 同德县| 合山市| 永修县| 长治市| 金昌市| 岳西县| 娱乐| 大连市| 常德市| 汾阳市| 南昌县| 绵竹市| 礼泉县| 前郭尔| 塔河县| 怀集县|