Java中的IO流(四)

2023-07-11,

  上一篇《Java中的IO流(三)》把IO流中的文件及目录操作的对象File类记录了一下,本篇把本不属性IO流但又和IO流有关系的一个对象作一下记录,此对象本属于集合框架里的一个子集,即Properties类

  Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。之所以把这个类放在IO流中来记录,原因就是因为此类可保存在流中或从流中加载。

  先记录一下Properties集合的特点:

    1,集合中的键和值都是字符串类型

    2,集合中的数据可以保存在流中,或从流中加载

  通常该集合用于操作以键值对形式存储的配置文件

一,Properties的基本操作-存-取-改

     private static void propertiesDemo1() {
Properties prop = new Properties();
// 往Properties中存数据用setProperties
prop.setProperty("zw", "23");
prop.setProperty("ls", "21");
// 从Properties中取数据
Set<String> set = prop.stringPropertyNames();// 获取Properties集合中所有的键的集合
//循环读取键名合集中的所有键,然后用getProperty(键名)获取元素
for (String str : set) {
System.out.println(prop.getProperty(str));
}
//修改元素用setProperty("键名相同","值不同");
}

二,Properties的集合方法-list(PrintSteam out)

  此方法的作用是将Properties集合中所存储的内容输出到所指定的输出流,即输出到所传递到list方法的参数out流,这个方法对调试很有用

     private static void propertiesDemo2() {
Properties prop = new Properties();
// prop.setProperty("zw", "23");
// prop.setProperty("ls", "21");
prop = System.getProperties();// 获取系统的配置信息Properties集合
prop.list(System.out);
}

三,将集合中的内容持久化到文件中-store

  此方法有两个重载的版本,接收两个参数,第一个参数是流的类型;第二个参数是写信文件的注释信息,此注释信息不支持中文,若是中文则以\uxxxx的形式写入

     private static void propertiesDemo3() throws IOException {
Properties prop = System.getProperties();
FileWriter writer = new FileWriter("properties.properties");
prop.store(writer, "系统信息");
}

四,将文件中的信息读取到Properties集合中-load

  此文件中的信息必须是以键值对的形式存在的,此方法也有两个重载的版本,接收的参数流类型不一样

     private static void propertiesDemo4() throws IOException {
Properties prop = new Properties();
Reader reader = new FileReader("properties.properties");
prop.load(reader);//加载文件到集合中
prop.list(System.out);//把集合中的所有内容输出到控制台
}

五,将键值对的集合数据持久化成XML形式的文件-storeToXML

  此方法有两个重载的版本接收三个参数的版本可以指定编码格式  

     private static void propertiesDemo5() throws IOException {
Properties prop = System.getProperties();
OutputStream outputStream = new FileOutputStream("a.xml");
prop.storeToXML(outputStream, "这是一个XML文件", "utf-8");
}

六,将XMl类型的配置文件读取到集合中

   private static void propertiesDemo6() throws InvalidPropertiesFormatException, IOException {
Properties prop = new Properties();
InputStream inputStream = new FileInputStream("a.xml");
prop.loadFromXML(inputStream);
prop.list(System.out);
}

七,读取配置文件的信息并修改其中的内容然后存储该修改后的配置文件信息

     private static void propertiesDemo7() throws IOException {
Properties prop = new Properties();
File file = new File("properties.properties");
if (!file.exists()) {
file.createNewFile();
}
Reader reader = new FileReader(file);
prop.load(reader);
Set<String> set = prop.stringPropertyNames();
for (String str : set) {
if (str.equals("os.version")) {
prop.setProperty(str, "8.0");
}
}
prop.store(new FileWriter("properties.properties"), "new config");
}

八,模拟软件试用的功能,即试用五次,然后提示用户注册

     private static void propertiesDemo8() throws IOException {
File file = new File("config.properties");
if (!file.exists()) {
file.createNewFile();
}
Reader inputStream = new FileReader(file);
Properties properties = new Properties();
properties.load(inputStream);
Writer writer = new FileWriter(file);
String count = properties.getProperty("count");
try {
if (count == null || count.equals("")) {
properties.setProperty("count", "0");
properties.store(writer, "First time");
} else {
int countTemp = Integer.parseInt(count);
countTemp++;
properties.setProperty("count", String.valueOf(countTemp));
properties.store(writer, "new time ");
if (countTemp >= 5) {
throw new RuntimeException("试用次数已到,请注册");
} }
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
inputStream.close();
writer.close();
}
}

Java中的IO流(四)的相关教程结束。

《Java中的IO流(四).doc》

下载本文的Word格式文档,以方便收藏与打印。