How does java read a string in a file and create an object of the class named after this character?
We generally use properties or XML files as resource storage files. Now we will mainly introduce the operations on properties.
1. Create a new package named config In the src file.
2. Create a file under config and name it demo. properties.
3. Copy the following text to demo. properties.
#commentsusername=oseye
4. Create a new class named RunMain. java, and add the textProperties method in it 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 is implemented in demo. properties reads the value of System. out. println ("username value:" + username );}
5. create a new class and take the class name as needed. I created a class named "People", and at least two classes need to be implemented by changing the demo. propertie to create 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); public abstract void setHigh (String high );}
People1:
Package config; public class People1 extends People {private String age; public People1 () {super (); // TODO Auto-generated constructor stub} public People1 (String sname, String sex, string age) {super (sname, sex); this. age = age; // TODO Auto-generated constructor stub} // getset method @ Overridepublic String toString () {return "lele1 [name =" + super. getSname () + ", sex =" + super. getSex () + ", age =" + age + "]" ;}}
People2:
package config;public class People2 extends People {private String high ;public People2() {super();// TODO Auto-generated constructor stub}public People2(String sname, String sex,String high) {super(sname, sex);this.high = high;// TODO Auto-generated constructor stub}@Overridepublic String toString() {return "People2 [name=" + super.getSname()+ ",sex="+super.getSex()+",high=" + high + "]";}@Overridepublic void setAge(String age) {// TODO Auto-generated method stub}@Overridepublic void setHigh(String high) {// TODO Auto-generated method stubthis.high=high;}}
6. Create a method named 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 demo. properties and add the class we want to create.
People = People2
8. Run the textAll () method in the main method, change it to People1, and run it again. The two output information of their respective classes respectively.
9. Now we have implemented reading a string in the file and creating a class object named after this character.