新建女孩類
public class Girl implements Serializable {
//transient修飾的屬性不能被序列化
private transient String name;
private String gender;
private int age;
public Girl() {
}
public Girl(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Girl{" +
"name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
'}';
}
}
從Properties文件中讀數據
@Test
void test1() throws IOException {
// Properties類:對 Properties文件操作的類
Properties properties = new Properties();
//加載文件//路徑
FileReader fileReader = new FileReader("src/main/resources/a.properties");
properties.load(fileReader);
//獲取數據
String name = properties.getProperty("a.name");
System.out.println("name=" + name);
//獲取數據,如果key不存在,返回默認值
System.out.println(properties.getProperty("height", "180"));
System.out.println(properties.propertyNames());
//獲取所有的key
System.out.println(properties.stringPropertyNames());
//遍歷properties文件的內容
for (String Key : properties.stringPropertyNames()) {
String value = properties.getProperty(Key);
System.out.printf("%s=%s\n", Key, value);
}
}
通過Properties生成對象,默認log4j 通過配置文件產生對象
@Test
void test2() throws IOException {
// Properties類:對 Properties文件操作的類
final Properties properties = new Properties();
//加載文件//路徑
FileReader fileReader = new FileReader("src/main/resources/log4j.properties");
properties.load(fileReader);
獲取指定key的value值
String zoo = properties.getProperty("zoo");
System.out.println(zoo);
獲取所有動物的名,用逗號隔開
String[] strings = zoo.split(",");
System.out.println(Arrays.toString(strings));
Stream.of(strings).skip(1).forEach(x -> {
String classname = properties.getProperty(x + ".class");
try {
Class<?> aClass = Class.forName(classname);
Constructor<?> constructor = aClass.getConstructor();
Object o = constructor.newInstance();
String nickname = properties.getProperty(x + ".nickname");
//
Method nicknameMethod = aClass.getMethod("setNickname", String.class);
nicknameMethod.invoke(o, nickname);
int weight = Integer.parseInt(properties.getProperty(x + ".weight"));
Method weightMethod = aClass.getMethod("setWeight", int.class);
weightMethod.invoke(o, weight);
System.out.println(o);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
});
}
//
向Properties文件中寫數據
@Test
void test3() throws IOException {
Properties properties = new Properties();
properties.setProperty("a", "456");
properties.setProperty("b", "56");
FileWriter fileWriter = new FileWriter("src/main/resources/b.properties", true);//在後面添加,不是覆蓋
properties.store(fileWriter, "第一次提交:");
}
@Test
void test4() throws IOException {
//創建一個Girl對象,並把對象存入文件666
Girl girl = new Girl("李若彤", "女", 19);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("src/main/resources/666"));
objectOutputStream.writeObject(girl);
}
@Test
void Test5() throws IOException, ClassNotFoundException {
FileInputStream fileInputStream = new FileInputStream("src/main/resources/666");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Object o = objectInputStream.readObject();
if (o instanceof Girl) {
Girl girl = (Girl) o;
System.out.println(girl);
}
}