序列化:将对象按照流的方式存储到文本文件中或者再网络中传输 对象---->流数据
序列化流
* (ObjectOutputStream)
* 反序列化:将文本文件中的流对象或者网络传输中的流对象还原成对象 流数据--->对象 反序列化流
* (ObjectInputStream)
举例:
public Object setProperty(String key, String value) :给属性列表中添加键和值,并且强制都使用String
public Set<String> stringPropertyNames():遍历的功能
public String getProperty(String key)用指定的键在此属性列表中搜索属性
举例:
public void store(Writer writer,String comments):把集合中的数据保存文本文件中(属性集合)
public void load(Reader reader):将文本文件中的数据加载到属性集合中
举例:
我有一个文本文件(user.txt),我知道数据是键值对形式的,但是不知道内容是什么。
请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其实为”100
1)读取文件的内容,将文件内容加载属性集合类中
2)遍历属性集合,获取所有的键的集合
3)遍历的键的时候,可以判断是否有"lisi"这样一个键
4)有的话,就更改
5)需要将当前属性集合类中的保存文本文件中
举例:
我有一个猜数字小游戏的程序,请写一个程序实现在测试类中只能用5次,
* (ObjectOutputStream)
* 反序列化:将文本文件中的流对象或者网络传输中的流对象还原成对象 流数据--->对象 反序列化流
* (ObjectInputStream)
举例:
public class ObjectDemo {
public static void main(String[] args) throws Exception {
//序列化
//write();
reader();
}
private static void reader() throws IOException, IOException, Exception {
ObjectInputStream ois=new ObjectInputStream(
new FileInputStream("oos.txt"));
Object object = ois.readObject();
ois.close();
System.out.println(object);
}
private static void write() throws IOException, IOException {
//创建一个序列化的流
ObjectOutputStream oos=new ObjectOutputStream(
new FileOutputStream("oos.txt"));
//创建对象
Person p=new Person("曾轶可",27);
oos.writeObject(p);
oos.close();
}
}
属性集合类的特有功能:
public Object setProperty(String key, String value) :给属性列表中添加键和值,并且强制都使用String
public Set<String> stringPropertyNames():遍历的功能
public String getProperty(String key)用指定的键在此属性列表中搜索属性
举例:
public class PropertiesDemo2 {
public static void main(String[] args) {
Properties p=new Properties() ;
p.setProperty("太阳", "地球");
p.setProperty("星星", "月亮");
p.setProperty("大地", "白云");
Set<String> KeySet = p.stringPropertyNames();
for(String key:KeySet) {
String value = p.getProperty(key);
System.out.println(key+"---"+value);
}
}
}
可保存在流中或从流中加载,只能使用属性集合类
public void store(Writer writer,String comments):把集合中的数据保存文本文件中(属性集合)
public void load(Reader reader):将文本文件中的数据加载到属性集合中
举例:
public class PropertiesDemo3 {
public static void main(String[] args) throws IOException {
MyStore();
MyLoad();
}
private static void MyLoad() throws IOException {
Properties p1=new Properties();
FileReader fr=new FileReader("fw.txt");
p1.load(fr);
fr.close();
System.out.println(p1);
}
private static void MyStore() throws IOException {
Properties p=new Properties();
//将属性集合中的数据保存在文本中去
p.setProperty("张三", "27");
p.setProperty("李四", "26");
p.setProperty("王五", "25");
FileWriter fw=new FileWriter("fw.txt");
p.store(fw, "names'content");
fw.close();
}
}
我有一个文本文件(user.txt),我知道数据是键值对形式的,但是不知道内容是什么。
请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其实为”100
1)读取文件的内容,将文件内容加载属性集合类中
2)遍历属性集合,获取所有的键的集合
3)遍历的键的时候,可以判断是否有"lisi"这样一个键
4)有的话,就更改
5)需要将当前属性集合类中的保存文本文件中
举例:
public class PropertiesTest {
public static void main(String[] args) throws IOException {
//创建塑型集合类对象
Properties p=new Properties();
FileReader fr=new FileReader("fw.txt");
p.load(fr);
fr.close();
Set<String> keySet = p.stringPropertyNames();
for(String key:keySet) {
if("李四".equals(key)) {
p.setProperty(key,"100");
}
}
FileWriter fw=new FileWriter("user.txt");
p.store(fw, "names'comments");
fw.close();
}
}
我有一个猜数字小游戏的程序,请写一个程序实现在测试类中只能用5次,
超过5次提示:游戏试玩已结束,请付费。
举例:
public class PropertiesTest2 {
public static void main(String[] args) throws IOException {
Properties p=new Properties();
//将文件加载到属性集合类中
FileReader fr=new FileReader ("count.txt");
p.load(fr);
fr.close();
String value = p.getProperty("count");
int num = Integer.parseInt(value);
if(num>5) {
System.out.println("游戏试玩结束,请您付费");
}else {
num++;
p.setProperty("count", String.valueOf(num));
FileWriter fw=new FileWriter("count.txt");
p.store(fw, "game'content");
fw.close();
GetNum.start();
}
}
}