Still use code to speak ..
This is a Java Project
Package COM. jadyer. reflection; </P> <p> Import Java. io. filereader; <br/> Import Java. lang. reflect. method; <br/> Import Java. util. enumeration; <br/> Import Java. util. hashtable; <br/> Import Java. util. properties; </P> <p> Import COM. jadyer. model. student; </P> <p>/** <br/> * simulate the parameters of the JavaBean receiving form <br/> */<br/> public class imitatejavabean {<br/> Private Static hashtable <string, student> mysession; // simulate the session object in Web Programming </P> <p> static {<br/> mysession = new hashtable <string, student> (); <br/> mysession. put ("Stu", new student (); <br/>}</P> <p> Public static void main (string [] ARGs) throws exception {<br/> properties Properties = new properties (); <br/> filereader = new filereader ("props.txt"); <br/> properties. load (filereader); // import all attribute information in the specified file <br/> filereader. close (); // close the opened file </P> <p> enumeration <?> Propertiesnames = properties. propertynames (); // obtain all attribute names <br/> while (propertiesnames. hasmoreelements () {<br/> string name = (string) propertiesnames. nextelement (); <br/> string value = properties. getproperty (name); <br/> imitatejavabean. invokesetter ("Stu", name, value); // execute the setter method and assign a value to the attribute <br/>}</P> <p> system. out. println (imitatejavabean. mysession. get ("Stu"); // outputs the value of the element marked by Stu, that is, the student object <br/>}</P> <p>/** <Br/> * call the setter method of the specified attribute <br/> * @ Param beanname ------- key in mysession, you can obtain the corresponding JavaBean object <br/> * @ Param propertyname --- name in the external resource file, use it to construct the setter method <br/> * @ Param propertyvalue -- Value in the external resource file, use it to specify the constructed setter parameter value <br/> */<br/> Public static void invokesetter (string beanname, string propertyname, string propertyvalue) throws exception {<br/> Object OBJ = mysession. get (beanname); <br/> class <?> Clazz = obj. getclass (); </P> <p> // dynamically construct a setter method, such as name --> setname <br/> // propertyname. substring () can retrieve the first letter of the attribute name <br/> // propertyname. substring (1) all other letters can be extracted from the first letter of the attribute name <br/> string methodname = "set" + propertyname. substring (0, 1 ). touppercase () + propertyname. substring (1); </P> <p> // method = clazz. getmethod (methodname, Java. lang. string. class) // You can also write <br/> method = clazz. getmethod (methodname, new class [] {Java. lang. string. class}); </P> <p> method. invoke (OBJ, propertyvalue); // use propertyvalue as the parameter value of the method, and then call this method <br/>}< br/>}
The following is the student class used.
Package COM. jadyer. model; </P> <p> public class student {<br/> private string name; <br/> private string password; </P> <p> Public void setname (string name) {<br/> This. name = Name; <br/>}< br/> Public void setpassword (string password) {<br/> This. password = password; <br/>}</P> <p> @ override <br/> Public String tostring () {<br/> return "Name: "+ name +"/tpassword: "+ password; <br/>}< br/>}
Finally, the program file props.txt is used in the Java projectroot directory.
Name = admin <br/> Password = jadyer