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 ";
//
////
/**
* New logic:
* 1. You must first read the original content of the file.
* 2. Add new records and save them together.
*/
// 1. Read the original file content first
Inputstream in = sendemail. Class. getresourceasstream (filepath );
// If JSP is used to read the file, but the file content is not changed if the server is not restarted after the attribute is added or modified.
// If the file is modified, you must use the following method to change the page in time.
// URL url = sendemail. Class. getresource (filepath );
// 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. Add the attribute content you want to add
//// 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 === ");
/**
* Modify the logic: just reset the value of the corresponding key, which is very simple.
*/
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 = ");
/**
// * Deletion logic: Find the corresponding key and delete it.
//*/
// Prop. Clear ();
// Tosavemap. Remove ("name ");
// Prop. putall (tosavemap );
// Prop. Store (Out, "=== after remove === ");
//
/**
* Query logic: You know the drop
*/
Prop. Load (in );
System. Out. println ("name:" + Prop. Get ("name "));
System. Out. println ("Password:" + Prop. Get ("password "));
}
}