<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > We generally use properties or XML files as resources to store files, and now the main introduction to the properties of the Operation </span>
1. We create a new package called Config under the src file
2. Create a new file under config and name it demo.properties
3. Copy the following paragraphs into the demo.properties
#commentsusername =oseye
4. Create a new class called Runmain.java, and add the Textproperties method as follows:
Public static void Textproperties () {Properties Prop=new properties (); try {prop.load ( RunMain.class.getResourceAsStream ("/config/demo.properties"));} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} String username = prop.getproperty ("username");//This method implements the value username in Demo.properties read-out property to System.out.println (" The value of Username: "+username);}
5. Create a new class of your own, this class name is taken, I built a class called people, in addition to need at least two class implementation by changing the Demo.propertie for different objects, so I wrote People1 and People2 to inherit people:
People
Package Config;public Abstract class People {private string Sname;private string Sex;public people () {super ();//TODO Auto -generated constructor stub}public People (string sname, String sex) {super (); this.sname = Sname;this.sex = sex;} Get&set method @overridepublic String toString () {return "people [sname=" + sname + ", sex=" + Sex + "]";} public abstract void Setage (string age);p ublic abstract void Sethigh (String high);}
People1:
Package Config;public Class People1 extends people {private String Age;p ublic People1 () {super ();//TODO auto-generated C Onstructor stub}public People1 (String sname, String sex,string age) {super (sname, sex); this.age = age;//TODO Auto-generat Ed constructor Stub}//getset method @overridepublic String toString () {return "People1 [name=" + super.getsname () + ", sex=" + Super.getsex () + ", age=" + Age + "]";}}
People2:
Package Config;public Class People2 extends people {private String high;p ublic People2 () {super ();//TODO auto-generated Constructor Stub}public People2 (String sname, String sex,string High) {super (sname, sex); This.high = high;//TODO Auto-gen erated constructor stub} @Overridepublic String toString () {return "People2 [name=" + super.getsname () + ", sex=" + Super.getsex () + ", high=" + High + "]";} @Overridepublic void Setage (string age) {//TODO auto-generated method stubs} @Overridepublic void Sethigh (String high) {// TODO auto-generated method Stubthis.high=high;}}
6. Create a method called Textall in Runmain:
public static void Textall () {Properties Prop=new properties (); People p = null;try {prop.load (RunMain.class.getResourceAsStream ("/config/demo.properties")); Class people = class.forname ("config.") +prop.getproperty ("People"));p = (people) people.newinstance (); catch (ClassNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (Instantiationexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (Illegalaccessexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} P.setsex ("NV");p. Setsname ("Xww"); if (P instanceof People1) {p.setage ("20");} else if (p instanceof People2) {P.sethigh ("180");} SYSTEM.OUT.PRINTLN (P);}
7. Modify the value of the demo.properties to add the class we want to create
People=people2
8. Run the Textall () method in the main method, then change it to People1 and run it again, outputting the information of the respective class respectively.
9. At this point, we have implemented an object that reads a string in a file and creates a class with the name of the character.
How Java reads a string in a file and creates an object of the class with that character as its name