java如何在檔案中讀取一個字串並建立以這個字元為名字的類的物件
阿新 • • 發佈:2019-02-12
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">我們一般用properties或者XML檔案作為資源儲存的檔案,現在主要介紹一下對properties的操作</span>
1.我們在src檔案下新建一個名稱為config的包
2.在config下新建一個file,把它命名為demo.properties
3. 把下面的幾段文字複製到demo.properties中
#comments
username=oseye
4.新建一個叫RunMain.java的class ,在裡面新增textProperties方法如下:
5.新建一個自己的類,這個類名就隨便取了,我建了一個叫People的類,另外需要至少兩個類實現通過更改demo.propertie來建立不同的物件,所以我寫了People1和People2來繼承People:public static void textProperties(){ Properties prop=new Properties(); try { prop.load(RunMain.class.getResourceAsStream("/config/demo.properties")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String username = prop.getProperty("username");//此方法實現了在demo.properties讀出屬性為username的值 System.out.println("username的值:"+username); }
People
People1: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方法 @Override public String toString() { return "People [sname=" + sname + ", sex=" + sex + "]"; } public abstract void setAge(String age); public abstract void setHigh(String high); }
People2: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方法 @Override public String toString() { return "People1 [name=" + super.getSname()+ ",sex="+super.getSex()+",age=" + age + "]"; } }
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
}
@Override
public String toString() {
return "People2 [name=" + super.getSname()+ ",sex="+super.getSex()+",high=" + high + "]";
}
@Override
public void setAge(String age) {
// TODO Auto-generated method stub
}
@Override
public void setHigh(String high) {
// TODO Auto-generated method stub
this.high=high;
}
}
6.在RunMain中建立一個名稱為textAll的方法:
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 block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.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.修改demo.properties的值,新增上我們要建立的類
People=People2
8.在主方法中執行一下textAll()這個方法,再改成People1再執行一下,兩者分別輸出各自類的資訊
9.至此我們就實現了在檔案中讀取一個字串並建立以這個字元為名字的類的物件