Properties類
Properties類
Properties(配置檔案類): 主要用於生產配置檔案與讀取配置檔案的資訊。
往properties檔案寫入資料:
public class Demo3 { public static void main(String[] args) throws IOException { creatProperties(); } //儲存配置檔案檔案的資訊。 public static void creatProperties() throws IOException{ //建立Properties Properties properties = new Properties(); properties.setProperty("陳奕迅", "歌頌"); properties.setProperty("楊千嬅","再見二丁目"); properties.setProperty("謝安琪","年度之歌"); // 遍歷Properties /*Set<Entry<Object, Object>> entrys = properties.entrySet(); for(Entry<Object, Object> entry:entrys){ System.out.println("鍵:"+ entry.getKey() +" 值:"+ entry.getValue()); }*/ //使用Properties生產配置檔案。 //第一個引數是一個輸出流物件,第二引數是使用一個字串描述這個配置檔案的資訊。 //properties.store(new FileOutputStream("E:\\songs.properties"), "songList"); properties.store(new FileWriter("E:\\songs.properties"), "list"); } }
讀取Properties檔案的資料:
public class Demo3 { public static void main(String[] args) throws IOException { //creatProperties(); readProperties(); } //讀取配置檔案的資訊 public static void readProperties() throws IOException{ //建立Properties物件 Properties properties = new Properties(); //載入配置檔案資訊到Properties中 properties.load(new FileReader("F:\\songList.properties")); //遍歷 Set<Entry<Object, Object>> entrys = properties.entrySet(); for(Entry<Object, Object> entry:entrys){ System.out.println("鍵:"+ entry.getKey() +" 值:"+ entry.getValue()); } //修改陳奕迅的歌曲 //把修改後的Properties再生成一個配置檔案 properties.setProperty("陳奕迅", "全世界失眠"); properties.store(new FileWriter("F:\\\\songList.properties"), "newList"); } //儲存配置檔案檔案的資訊。 public static void creatProperties() throws IOException{ //建立Properties Properties properties = new Properties(); properties.setProperty("陳奕迅", "歌頌"); properties.setProperty("楊千嬅","再見二丁目"); properties.setProperty("謝安琪","年度之歌"); // 遍歷Properties /*Set<Entry<Object, Object>> entrys = properties.entrySet(); for(Entry<Object, Object> entry:entrys){ System.out.println("鍵:"+ entry.getKey() +" 值:"+ entry.getValue()); }*/ //使用Properties生產配置檔案。 //第一個引數是一個輸出流物件,第二引數是使用一個字串描述這個配置檔案的資訊。 //properties.store(new FileOutputStream("E:\\songs.properties"), "songList"); properties.store(new FileWriter("F:\\songs.properties"), "list"); } }
Properties要注意的細節:
- 如果配置檔案的資訊一旦使用了中文,那麼在使用store方法生成配置檔案的時候只能使用字元流解決,如果使用位元組流生成配置檔案的話,預設使用的是iso8859-1碼錶進行編碼儲存,這時候會出現亂碼。
- 如果Properties中的內容發生了變化,一定要重新使用Properties生成配置檔案(store),否則配置檔案資訊不會發生變化。
store(OutputStream out,String comments);
store(Writer writer,String comments);
引數一:輸出流物件
引數二:使用一個字串描述這個配置檔案的資訊。
使用properties檔案記錄軟體的執行次數,超過三次之後提示購買正版,退出JVM:
public class Demo { public static void main(String[] args) throws IOException { File file = new File("F:\\count.properties"); if(!file.exists()){ //如果配置檔案不存在,則建立該配置檔案 file.createNewFile(); } //建立Properties物件。 Properties properties = new Properties(); //把配置檔案的資訊載入到properties中 properties.load(new FileInputStream(file)); //此處注意建立輸出流要在載入了properties之後,因為每次建立流,都會把之前的內容清空。 FileOutputStream fileOutputStream = new FileOutputStream(file); int count = 0; //定義該變數是用於儲存軟體的執行次數的。 //讀取配置檔案的執行次數 String value = properties.getProperty("count"); if(value!=null){ count = Integer.parseInt(value); } //判斷使用的次數是否已經達到了三次, if(count==3){ System.out.println("您已經超出試用次數,請購買正版軟體!!"); System.exit(0); } count++; System.out.println("你已經使用了本軟體第"+count+"次"); properties.setProperty("count",count+""); //使用Properties生成一個配置檔案 properties.store(fileOutputStream,"runtime"); } }
實際開發中的使用
Props工具類:用來讀取Properties檔案中的資訊
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.Properties; public class Props { private static String configPath = "C:\\config\\config.properties"; public static String getConfigPath() { return configPath; } public static void setConfigPath(String configPath) { Props.configPath = configPath; } private static Properties props = new Properties(); private static void loadProperty() { InputStream ins = null; try { ins = new BufferedInputStream(new FileInputStream(configPath)); props.load(ins); } catch (IOException e) { e.printStackTrace(); } finally { if (ins != null) { try { ins.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static String getString(String key) { if (props.get(key) == null) { loadProperty(); } return (String) props.get(key); } public static boolean getBoolean(String key) { if (props.get(key) == null) { loadProperty(); } String val = (String) props.get(key); return Boolean.parseBoolean(val); } public static int getInt(String key) { if (props.get(key) == null) { loadProperty(); } String val = (String) props.get(key); return Integer.parseInt(val); } }