Read properties from Java and JSP

Source: Internet
Author: User

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 "));

}

 

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.