package com.hx.shopping.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.Set;
import com.hx.shopping.util.PropUtil;
/**
* 属性集类
* Properties类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
* 特点:继承于HashTable是线程安全的键值对存储结构
* Properties 可保存在流中或从流中加载
* 只能保存字符串的键值对
*/
public class PropertiesTest {
public static void main(String[] args) throws IOException {
//属性集类不支持泛型
Properties prop = new Properties();//Properties():创建一个无默认值的空属性列表。
//添加属性
prop.setProperty("name", "大明");
prop.setProperty("age", "33");
prop.setProperty("gender", "male");
//获取属性
String name = prop.getProperty("name");
System.out.println(name);
System.out.println(prop);//{age=13, name=小明}
System.out.println(prop.values());//打印所有的值: [33, 大明, male]
/**
* 读取(配置)文件的三种方式
*/
FileReader fr = null;
InputStream is =null;
try {
//第一种:
fr = new FileReader("prop3.txt");
//第二种:
is = new FileInputStream("prop3.txt");//实际项目中使用properties文件来存储数据,不使用txt 如:fr = new FileReader("src/jdbc.properties");
//把字符流数据读到prop对象中
prop.load(is);
//获取数据
String name2 = prop.getProperty("name");
System.out.println(name2);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(is!=null){
is.close();
}
}
//第三种:
if(prop==null){
prop = new Properties();
try {
/*
* 查找具有给定名称的资源:
* PropUtil.class.getClassLoader()得到src根路径
* getResourceAsStream():读取fileName中的键值对集,返回一个InputStream
* load:从输入流中读取属性列表(键和元素对)到prop对象
* 注:此时的jdbc.properties配置文件放在src目录下
*/
prop.load(PropUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建文件的两种方式:
*/
FileWriter fw = null;
PrintWriter pw = null;
try {
//创建文件(输入流)
fw = new FileWriter("prop3.txt");
pw = new PrintWriter("prop2.txt");//(new FileWriter("prop.txt"),true)
//获取prop对象中所有的键
Set keys = prop.keySet();
for(Object obj:keys){
String key = (String)obj;
String value = prop.getProperty(key);
pw.println(key+"="+value);
}
//将属性存入文件
prop.store(fw, null);
/*#Sat Apr 21 15:23:53 CST 2018
age=33
name=sdgadg
gender=male*/
prop.list(pw);//list():将属性列表输出到指定的输出流。
/*即:将全部属性列入prop2.txt文件
-- listing properties --
age=13
name=小明
gender=male*/
} catch (Exception e) {
e.printStackTrace();
} finally{
if(fw!=null){
//关闭流
fw.close();
}
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.Set;
import com.hx.shopping.util.PropUtil;
/**
* 属性集类
* Properties类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
* 特点:继承于HashTable是线程安全的键值对存储结构
* Properties 可保存在流中或从流中加载
* 只能保存字符串的键值对
*/
public class PropertiesTest {
public static void main(String[] args) throws IOException {
//属性集类不支持泛型
Properties prop = new Properties();//Properties():创建一个无默认值的空属性列表。
//添加属性
prop.setProperty("name", "大明");
prop.setProperty("age", "33");
prop.setProperty("gender", "male");
//获取属性
String name = prop.getProperty("name");
System.out.println(name);
System.out.println(prop);//{age=13, name=小明}
System.out.println(prop.values());//打印所有的值: [33, 大明, male]
/**
* 读取(配置)文件的三种方式
*/
FileReader fr = null;
InputStream is =null;
try {
//第一种:
fr = new FileReader("prop3.txt");
//第二种:
is = new FileInputStream("prop3.txt");//实际项目中使用properties文件来存储数据,不使用txt 如:fr = new FileReader("src/jdbc.properties");
//把字符流数据读到prop对象中
prop.load(is);
//获取数据
String name2 = prop.getProperty("name");
System.out.println(name2);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(is!=null){
is.close();
}
}
//第三种:
if(prop==null){
prop = new Properties();
try {
/*
* 查找具有给定名称的资源:
* PropUtil.class.getClassLoader()得到src根路径
* getResourceAsStream():读取fileName中的键值对集,返回一个InputStream
* load:从输入流中读取属性列表(键和元素对)到prop对象
* 注:此时的jdbc.properties配置文件放在src目录下
*/
prop.load(PropUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建文件的两种方式:
*/
FileWriter fw = null;
PrintWriter pw = null;
try {
//创建文件(输入流)
fw = new FileWriter("prop3.txt");
pw = new PrintWriter("prop2.txt");//(new FileWriter("prop.txt"),true)
//获取prop对象中所有的键
Set keys = prop.keySet();
for(Object obj:keys){
String key = (String)obj;
String value = prop.getProperty(key);
pw.println(key+"="+value);
}
//将属性存入文件
prop.store(fw, null);
/*#Sat Apr 21 15:23:53 CST 2018
age=33
name=sdgadg
gender=male*/
prop.list(pw);//list():将属性列表输出到指定的输出流。
/*即:将全部属性列入prop2.txt文件
-- listing properties --
age=13
name=小明
gender=male*/
} catch (Exception e) {
e.printStackTrace();
} finally{
if(fw!=null){
//关闭流
fw.close();
}
}
}
}