1.首先在原始碼根目錄(src下)下建立一個名為netconfig.properties的檔案(也可以在其他目錄下)。
2.開啟netconfig.properties檔案,在該檔案中添加下列代碼.
Java代碼:
#該處為注釋 IP=192.168.1.111 PORT=3535 MSG=這是一個很長的字串,<br>我們可以使其換行 \ 一行顯示不下時,<br>可以分行顯示。 #該處為注釋IP=192.168.1.111PORT=3535MSG=這是一個很長的字串,<br>我們可以使其換行 \一行顯示不下時,<br>可以分行顯示。
說明:上面代碼中,#為設定檔中的注釋,而在設定檔中,可以使用<br>等進行格式處理,在設定檔中,如果某個屬性過長,一行不能輸入完全是,可以通過 \ 通知系統,下一行同樣為該屬性的值。
3.之後建立一個工具類Utils類,其代碼如下所列。
** * @description: 工具類 * @version 1.0 * @created on 2010-12-3 下午02:39:52 */ public class Utils { /** * 得到netconfig.properties設定檔中的所有配置屬性 * * @return Properties對象 */ public static Properties getNetConfigProperties() { Properties props = new Properties(); InputStream in = Utils.class.getResourceAsStream("/netconfig.properties"); try { props.load(in); } catch (IOException e) { e.printStackTrace(); } return props; } }
4.在需要使用設定檔中配置的屬性值時,直接調用上述方法即可,如下所示。
String IP = Utils.getNetConfigProperties().getProperty("IP");