package com.strongit.mail;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Test {
@SuppressWarnings({ "unchecked", "unchecked", "unchecked" })
public static void main(String args[]) throws IOException {
Properties prop = new Properties();
String filePath = "info.properties";
//
////
/**
* 新增邏輯:
* 1.必須先讀取檔案原有內容
* 2.增加新的記錄以後,再一起儲存
*/
//1.先讀取檔案原有內容
InputStream in =Sendemail.class.getResourceAsStream(filePath);
//此處如果用這個方法jsp讀取檔案,但屬性增加或者修改後,如果沒有重啟伺服器,讀取的檔案內容並沒有被改變
//如果檔案修改後,要求顯示頁面有及時變換用下面的方法
// URL url =Sendemail.class.getResource(filePath);
// File file =null;
// try {
// file = new File(url.toURI());
// } catch (URISyntaxException e) {
// e.printStackTrace();
// }
// InputStream in = new FileInputStream(file);
prop.load(in);
Map<String, Object> toSaveMap = new HashMap();
Set keys = prop.keySet();
for(Iterator itr = keys.iterator(); itr.hasNext();){
String key = (String) itr.next();
Object value = prop.get(key);
System.out.println(value);
toSaveMap.put(key, value);
}
// //2.增加你需要增加的屬性內容
// //toSaveMap.put("MailServerHost", "mail.126.com");
// toSaveMap.put("sender_email", "killkill@126.com");
// toSaveMap.put("receiver_email1", "b@126.com");
// prop.putAll(toSaveMap);
// prop.store(out, "==== after add ====");
/**
* 修改邏輯:重新設定對應Key的值即可,非常簡單
*/
prop.clear();
//toSaveMap.put("MailServerHost", "mail.126.com");
// toSaveMap.put("sender_email", "c@nanhai.gov.cn");
// toSaveMap.put("receiver_email1", "b@126.com");
toSaveMap.put("receiver_email2", "a@126.com");
// toSaveMap.put("name", "killkill");
//toSaveMap.put("password", "12345678");
prop.putAll(toSaveMap);
URL url =Sendemail.class.getResource(filePath);
File file =null;
try {
file = new File(url.toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
OutputStream out = new FileOutputStream(file);
prop.store(out, "==== after modify ====");
/**
// * 刪除邏輯:找到對應的key,刪除即可
// */
// prop.clear();
// toSaveMap.remove("name");
// prop.putAll(toSaveMap);
// prop.store(out, "==== after remove ====");
//
/**
* 查詢邏輯:你是知道滴
*/
prop.load(in);
System.out.println("name: " + prop.get("name"));
System.out.println("password: " + prop.get("password"));
}
}