第20天-11-IO流(Properties简述)

第20天-12-IO流(Properties存取)

package bxd;

import java.io.IOException;
import java.util.Properties;
import java.util.Set; /*
Properties是hashtable的子类, 也就是说它具备map集合的特点, 而且它里面存储的键值对都是字符串, 是集合中和IO技术相结合的集合容器.
该对象的特点: 可以用于键值对形式的配置文件
*/
public class PropertiesDemo_1 { // 以下方法没有关联文件, 都是在内存对象中进行操作
public static void setAndGet() {
Properties properties = new Properties(); // 设置Properties元素
properties.setProperty("zhangsan", "30");
properties.setProperty("lisi", "40");
System.out.println(properties); // 获取Properties值
System.out.println("getProperty(\"zhangsan\"): " + properties.getProperty("zhangsan"));
System.out.println("getProperty(\"lisi\"): " + properties.getProperty("lisi")); // 修改Properties值
properties.setProperty("lisi", "89");
Set<String> names = properties.stringPropertyNames();
for (String s : names) {
System.out.println(s + ":" + properties.getProperty(s));
}
} public static void main(String[] args) throws IOException {
setAndGet();
}
}

第20天-13-IO流(Properties存取配置文件)

package bxd;

import java.io.*;
import java.util.Properties; public class PropertiesDemo_2 { // 演示: 如何将流中的数据存储到集合中, 想要将message.properties中键值数据存到集合中继续操作
// 1. 用一个输入流和文件进行关联
// 2. 读取一行数据, 将该行数据以"="作为分隔符进行切分, 结果存入字符串数组
// 3. 等号左边为键, 右边为值, 存入到Properties对象中
public static void method_1() throws IOException {
Properties properties = new Properties();
BufferedReader bufr = new BufferedReader(new FileReader("messages.properties")); String line;
while ((line = bufr.readLine()) != null) {
String[] arr = line.split("=");
properties.setProperty(arr[0], arr[1]);
}
System.out.println(properties);
} public static void loadDemo() throws IOException {
Properties properties = new Properties();
FileInputStream fis = new FileInputStream("messages.properties"); // 加载
properties.load(fis);
System.out.println(properties); // 打印, This method is useful for debugging.
properties.list(System.out);
} public static void storeDemo() throws IOException {
Properties properties = new Properties();
FileOutputStream fileOutputStream = new FileOutputStream("info.txt");
properties.setProperty("key_one", "value_one"); // 保存
properties.store(fileOutputStream, "just for test");
} public static void main(String[] args) throws IOException {
storeDemo();
}
}

第20天-14-IO流(Properties练习)

package bxd;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties; /*
键值对数据是map集合, 数据是以文件形式存储, 使用IO技术, 即map + IO --> Properties; 如果处理xml, 则常常使用dom4j.
*/
public class PropertiesDemo_3 { public static void main(String[] args) throws IOException {
Properties properties = new Properties();
File file = new File("info.txt"); if (!file.exists()) file.createNewFile();
properties.load(new FileInputStream(file)); int count = 0;
String value = properties.getProperty("time");
if (value != null) {
count = Integer.parseInt(value);
if (count >= 5) {
System.out.println("对不起, 使用次数已到!");
return;
}
}
count++; properties.setProperty("time", count + "");
properties.store(new FileOutputStream(file), " ");
}
}
05-11 22:55