Hello friend, I'm |

Ritchie里其

园龄:2年6个月粉丝:4关注:7

练习题09-Proerties、IO

案例需求:
在Properties文件中手动写上姓名和年龄,读取到集合中,将该数据封装成学生对象,写到本地文件
实现步骤:
1.创建Properties集合,将本地文件中的数据加载到集合中
2.获取集合中的键值对数据,封装到学生对象中
3.创建序列化流对象,将学生对象序列化到本地文件中

package com.xxx;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtils {
private Properties pro;
private static PropertiesUtils propertiesUtils;
private PropertiesUtils() {
try {
pro = new Properties();
//加载配置文件的信息到Properties对象中
InputStream is = new FileInputStream("student.properties");
//InputStreamReader isr = new InputStreamReader(new FileInputStream("student.properties"), "GBK");
pro.load(is);
} catch (IOException e) {
e.printStackTrace();
}
}
public static PropertiesUtils getInstance() {
if (propertiesUtils == null) {
propertiesUtils = new PropertiesUtils();
}
return propertiesUtils;
}
//定义一个方法根据键获取值
public String getValue(String key) {
return pro.getProperty(key);
}
}
package com.xxx;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class TestPropertiesUtils {
public static void main(String[] args) throws IOException {
String name = PropertiesUtils.getInstance().getValue("name");
String age = PropertiesUtils.getInstance().getValue("age");
//实例化学生对象
Student stu = new Student(name,Integer.parseInt(age));
//实例化对象流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("stu02.txt"));
oos.writeObject(stu);
oos.close();
}
}

本文作者:Ritchie里其

本文链接:https://www.cnblogs.com/wang-zeyu/p/16819955.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Ritchie里其  阅读(57)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
✨欢迎你~🍻
✨欢迎你~🍻
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 遥か Aimer
遥か - Aimer
00:00 / 00:00
An audio error has occurred.

作词 : aimerrhythm/田中ユウスケ

作曲 : 田中ユウスケ

编曲 : 玉井健二/百田留衣

海岸線の雨に ちらばった君の影

思い出が交差する 海辺の街

君はあの日のまま いまも夢を見てた

君はあの日のまま いまも夢を見てた

遥か記憶の空 2人照らす光

遥か記憶の空 2人照らす光

膝までの浅瀬で 見つけた星

君まで届くなんてさ ありえないような

浅い眠りの中で 深い夢から覚めて

浅い眠りの中で 深い夢から覚めて

裸足のまま駆けてく まばゆい星

君はあの日のまま どんな夢を見てた?

君はあの日のまま どんな夢を見てた?

遥か記憶の空 2人照らす光

遥か記憶の空 2人照らす光

いつまでもこうして 笑っててほしい

夜空に舞い上がる 幾千の花びら

でたらめな誓いで 生きてく日々

君から届くなんてさ ありえないような