漂在爪洼島上

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

          日歷

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          隨筆分類

          隨筆檔案

          新聞檔案

          我的連接

          搜索

          •  

          最新評論

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

          Posted on 2009-06-30 10:27 周競先 閱讀(1397) 評論(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!


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


          網站導航:
           
          主站蜘蛛池模板: 汽车| 玉溪市| 麦盖提县| 敦化市| 桃源县| 肇州县| 民乐县| 西丰县| 双牌县| 惠来县| 仪陇县| 沙河市| 苏尼特左旗| 广安市| 自治县| 榕江县| 武宣县| 广水市| 垦利县| 庆安县| 科技| 囊谦县| 花垣县| 通榆县| 堆龙德庆县| 紫阳县| 内黄县| 汽车| 乐昌市| 乌鲁木齐市| 景泰县| 通河县| 犍为县| 城市| 义马市| 小金县| 离岛区| 东乡| 余江县| 岳阳县| 茶陵县|