[摘錄]JAVA如何使用相對路徑

來源:互聯網
上載者:User
如何使用相對路徑

---摘自互連網

我們在程式中經常要用到資料檔案或資源檔(象),如果在程式中用絕對路徑,就會使程式失去靈活性。例如我們的應用程式目錄是D:\NECS,而資料目錄是 D:\NECS\DataBase,如果我們使用資料檔案時用D:\NECS\DataBase目錄名。那麼當我們程式被移到C盤的時候,就會出錯,因為這時資料目錄變成了C:\NECS\DataBase。所以我們要使用相對目錄 \DataBase。

我們可以使用類java.util.Properties ,該類是一個Hashtable,儲存許多系統屬性。其中:

……
java.home Java installation directory
java.class.path Java class path
file.separator File separator ("/" on UNIX)
user.dir User's current working directory
……

我們可以用java.lang.System的方法getProperty(String key)來獲得所需的屬性。

curDir = System.getProperty("user.dir");
fileBar= System.getProperty("file.separator");

則上例中的資料目錄,可表示為 curDir+fileBar+"DataBase"

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.