新建女孩类
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);
}
}