Properties(配置文件类):主要用于生产配置文件与读取配置文件的信息。


Properties要注意的细节:


    1.若果配置文件的信息一旦使用了中文,那么在使用store方法生成配置文件的时候只能使用字符流解决,如果使用字节流生成配置文件的话,


        默认使用的是iso-8859-1码表进行编码存储,这时候会出现乱码。

    2.如果Properties中的内容发生了变化,一定要重新使用Properties生成配置文件,否则配置文件信息不回发生变化。

package com.cn.properties;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import javax.sound.sampled.AudioFormat.Encoding;
/**
* Author:Liu Zhiyong
* Version:Version_1
* Date:2016年7月31日20:11:10
* Desc:
Properties(配置文件类):主要用于生产配置文件与读取配置文件的信息。
Properties要注意的细节:
1.若果配置文件的信息一旦使用了中文,那么在使用store方法生成配置文件的时候只能使用字符流解决,如果使用字节流生成配置文件的话,
默认使用的是iso-8859-1码表进行编码存储,这时候会出现乱码。
2.如果Properties中的内容发生了变化,一定要重新使用Properties生成配置文件,否则配置文件信息不回发生变化。
*/
public class Demo1 {
public static void main(String[] args) throws IOException {
createProperties();
readProperties();
}

//保存配置文件的信息
public static void createProperties() throws IOException, IOException{
//创建Properties
Properties properties = new Properties();
properties.setProperty("木丁西", "1234");
properties.setProperty("刘先森", "2343");
properties.setProperty("木sir", "4344");
properties.setProperty("流先生", "6666");
properties.setProperty("Mr.liu", "9999");
Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();
Iterator<Entry<Object, Object>> iterator = entrySet.iterator();
while(iterator.hasNext()){
Entry<Object, Object> next = iterator.next();
System.out.println(next.getKey() + "\t" + next.getValue());
}

//使用Properties生成配置文件
/* FileOutputStream fileOutputStream = new FileOutputStream("src/com/cn/properties/properties.properties");
properties.store(fileOutputStream, "这里存放的是用户名和密码");//第一个参数是输出流对象("."当前路径),第二个参数是使用一个字符串描述这个配置文件的信息。
fileOutputStream.close();
*/
FileWriter fileWriter = new FileWriter("src/com/cn/properties/properties.properties");
properties.store(fileWriter, "这里存放的是用户名和密码");
fileWriter.close();
}

//读取配置文件的信息
public static void readProperties() throws IOException{
//创建Properties
Properties properties = new Properties();
FileInputStream fileInputStream = new FileInputStream("src/com/cn/properties/properties.properties");
properties.load(fileInputStream);


Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();
Iterator<Entry<Object, Object>> iterator = entrySet.iterator();
while(iterator.hasNext()){
Entry<Object, Object> next = iterator.next();
System.out.println(next.getKey() + "\t" + next.getValue());
}

System.out.println("并没有修改成功==================================");
//修改密码 这里实际的文件无法修改成功。
properties.setProperty("木丁西", "改了");

Set<Map.Entry<Object, Object>> entrySet1 = properties.entrySet();
Iterator<Entry<Object, Object>> iterator1 = entrySet1.iterator();
while(iterator1.hasNext()){
Entry<Object, Object> next = iterator1.next();
System.out.println(next.getKey() + "\t" + next.getValue());
}

System.out.println("修改成功==================================");
//把修改后的Properties文件再生成一个配置文件
properties.store(new FileOutputStream("src/com/cn/properties/properties.properties"), "2222222222222");
Set<Map.Entry<Object, Object>> entrySet2 = properties.entrySet();
Iterator<Entry<Object, Object>> iterator2 = entrySet2.iterator();
while(iterator2.hasNext()){
Entry<Object, Object> next = iterator2.next();
System.out.println(next.getKey() + "\t" + next.getValue());
}
}
}
package com.cn.properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
/**
* Author:Liu Zhiyong
* Version:Version_1
* Date:2016年8月1日12:41:29
* Desc:
需求:使用Properties实现本软件只能运行3次,超过3次后就提示购买正版,退出JVM
*/
public class Demo2 {
public static void main(String[] args) throws IOException {
File file = new File("src/com/cn/properties/record.properties");
if(!file.exists()){
//如果配置文件不存在,则创建该文件
file.createNewFile();
}

// FileOutputStream fileOutputStream = new FileOutputStream(file);//这句话若放在这里就会出错,创建前会先清空文件
//创建Properties对象
Properties properties = new Properties();
//把配置文件的信息加载到properties中
properties.load(new FileInputStream(file));
// FileOutputStream fileOutputStream = new FileOutputStream(file);//这句话若放在这里还是会出错
//定该变量用于保存软件的运行次数
int times = 0;
//读取配置文件中的登陆次数
String value =properties.getProperty("count");
if(value != null){
times = Integer.valueOf(value);
}

if(times >= 3){
System.out.println("您的次数已经用完,请购买正版。。");
System.exit(0);
}
// FileOutputStream fileOutputStream = new FileOutputStream(file);//这句话最早能放这里,在虚拟机退出前。 √
times++;

System.out.println("您已经使用了软件" + times + "次。");
properties.setProperty("count", Integer.toString(times));
//使用Properties生成一个配置文件
// properties.store(fileOutputStream, "the login times");
properties.store(new FileOutputStream(file), "the login times");
}
}