Java Properties的常见使用

Properties

  • Properties类及其方法

  • 打印JVM参数

  • 打印自定义.properties文件中的值

  • 获取值 getProperties

  • 写入到Properties文件

  • 存储和加载 xml 配置文件

一.Properties类及其方法

1.Properties简介

Properties 类位于 java.util.Properties ,是Java 语言的配置文件所使用的类,继承了Hashtable 类,以Map 的形式进行放置值, put(key,value) get(key)。 Xxx.properties 为Java 语言常见的配置文件,如数据库的配置 jdbc.properties, 系统参数配置 system.properties。

2. Properties方法

二. 打印JVM参数

JVM 中可以获取Properties, 来打印输出 JVM 所了解的属性值。用list() 方法,打印到控制台。

public static void main(String[] args) {
   //打印JVM中的属性值
   Properties properties = System.getProperties();
   properties.list(System.out);
}

三. 打印自定义.properties文件中的值

test.properties文件

account=222017602053039
password=016891

1.list 输出到控制台,绝对路径加载

//获取加载的属性类
public static final Properties config = new Properties();

public static void main(String[] args) throws IOException {
   //获得文件输入流
   InputStream inputStream = new >FileInputStream("G:\\Android_Studio\\AndroidStudioProjects\\JavaCode\\Java\\src\\main\\java\\swu\\xl\\day17_self_properties\\test.properties");
   //加载输入流文件信息
   config.load(inputStream);
   //根据key获取属性信息
   System.out.println("账户:"+config.getProperty("account")+" 密码:"+config.getProperty("password"));
   //打印信息
   config.list(System.out);
}

输出:
账户:222017602053039 密码:016891
-- listing properties --
password=016891
account=222017602053039

2.propertyNames 输出,class.getResourceAsStream 加载

public class PropertiesLoad {
   public static void main(String[] args) throws IOException {
       //获取属性类
       Properties config = new Properties();
       //获得文件输入流
       InputStream inputStream = PropertiesLoad.class.getResourceAsStream("test.properties");
       //加载输入流文件信息
       config.load(inputStream);
       //打印信息
       config.list(System.out);
   }
}

输出:
-- listing properties --
password=016891
account=222017602053039

3.stringPropertyNames 输出,class.getClassLoader().getResourceAsStream 加载

public class PropertiesLoadPath {
   public static void main(String[] args) throws IOException {
       //获取属性类
       Properties config = new Properties();
       //获得文件输入流
       InputStream inputStream = PropertiesLoadPath.class.getClassLoader().getResourceAsStream("swu/xl/day17_self_properties/test.properties");
       //加载输入流文件信息
       config.load(inputStream);
       //打印信息
       config.list(System.out);
   }
}

输出:
-- listing properties --
password=016891
account=222017602053039

4.getResourceAsStream获取项目下的指定资源

① 当前类名.class.getResourceAsStream(String path)

  • path 不以/开头时默认是从此类所在的包下取资源,以/开头则是从ClassPath根下(即/代表src)获取。但是在Android Studio中/代表模块名称/build/classes/java/main。本质上是通过path构造一个绝对路径,最终还是由ClassLoader获取资源。

  • 在同级目录下:com.x.y 下有类me.class ,同时在同级目录下有资源文件myfile.xml,则应使用me.class.getResourceAsStream("myfile.xml");

  • 在子目录下:例如:com.x.y 下有类me.class ,同时在 com.x.y.file 目录下有资源文件myfile.xml,则应该使用me.class.getResourceAsStream("file/myfile.xml");

② 当前类名.class.getClassLoader().getResourceAsStream(String path)

  • 默认是从ClassPath根下获取,path不能以/开头,最终是由ClassLoader获取资源。

  • 在同级目录,例如:com.x.y 下有类me.class 同时有资源文件myfile.xml,则应该使用me.class.getClassLoader.getResourceAsStream("com/x/y/myfile.xml");

  • 不在同级目录下,也不在子目录下:例如:com.x.y 下有类me.class ,同时在 com.x.file 目录下有资源文件myfile.xml,则应该使用me.class.getClassLoader.getResourceAsStream("com/x/file/myfile.xml");

四.获取值 getProperties

  • 输出时,getProperty() 接受一个String类型key值,输出Key值对应的value 值。

  • 如果没有key值,则输出 null 值。后面可以跟 default 值,如果没有key值,则输出设置的默认值。

public class GetProperty {
   public static void main(String[] args) throws IOException {
       //获取属性类
       Properties config = new Properties();
       //获得文件输入流
       InputStream inputStream = PropertiesLoad.class.getResourceAsStream("test.properties");
       //加载输入流文件信息
       config.load(inputStream);
       //打印信息
       String account = config.getProperty("account","000");
       String password = config.getProperty("password","000");
       System.out.println("账户:"+account+" 密码:"+password);
   }
}

五.写入到Properties文件

1.普通写入,中文时乱码,包括写入键值对时添加的注释出现中文也会乱码。

public class DefaultSet {
   public static void main(String[] args) throws IOException {
       //获取属性类
       Properties config = new Properties();
       //获得文件输入流
       InputStream inputStream = PropertiesLoad.class.getResourceAsStream("test.properties");
       //加载输入流文件信息
       config.load(inputStream);

       //设置值
       config.setProperty("score","拟稿");

       //将添加的键值对和以前的键值对一起写入properties文件并添加注释
      OutputStream outputStream = new FileOutputStream("G:\\Android_Studio\\AndroidStudioProjects\\JavaCode\\Java\\build\\classes\\java\\main\\swu\\xl\\day17_self_properties\\test.properties");
       config.store(outputStream,"修改后的信息");
   }
}

运行后test.properties文件的内容:
#\u4FEE\u6539\u540E\u7684\u4FE1\u606F
#Wed Sep 04 19:19:08 CST 2019
password=016891
account=222017602053039
score=\u62DF\u7A3F

2.解决写入乱码的问题,在构建输入流,输出流的同时,指定编码格式。注释内容还是会乱码。

public class UTF8Set {
   public static void main(String[] args) throws IOException {
       //获取属性类
       Properties config = new Properties();
       //获得文件输入流
       InputStream inputStream = PropertiesLoad.class.getResourceAsStream("test.properties");
       //加载输入流输出流的同时,指定编码格式
       config.load(new InputStreamReader(inputStream,"utf-8"));
       //设置值
       config.setProperty("score","拟稿");
       //将添加的键值对和以前的键值对一起写入properties文件并添加注释
       OutputStream outputStream = new FileOutputStream("G:\\Android_Studio\\AndroidStudioProjects\\JavaCode\\Java\\build\\classes\\java\\main\\swu\\xl\\day17_self_properties\\test.properties");
       config.store(new OutputStreamWriter(outputStream,"utf-8"),"修改后的信息");
   }
}

运行后test.properties文件的详细内容
#\u4FEE\u6539\u540E\u7684\u4FE1\u606F
#Wed Sep 04 19:37:35 CST 2019
password=016891
account=222017602053039
score=拟稿

六.存储和加载 xml 配置文件

1.导出 内存键值对数据 到 .xml 配置文件 ---storeToXML

public class StoreToXml {
   public static void main(String[] args) throws IOException {
       //获取属性类
       Properties config = new Properties();
       //设置值
       config.setProperty("account","222017602053039");
       config.setProperty("password","016891");
       //创建输出流
       OutputStream outputStream = new FileOutputStream("G:\\Android_Studio\\AndroidStudioProjects\\JavaCode\\Java\\src\\main\\java\\swu\\xl\\day17_self_properties\\test.xml");
       //存储到xml中并确定编码
       config.storeToXML(outputStream,"填充到xml","utf-8");
   }
}

运行后在给定的路径产生文件test.xml



填充到xml
016891
222017602053039

2. 导出 .xml配置文件数据 到 内存---loadFromXML

public class LoadFromXml {
   public static void main(String[] args) throws IOException {
       //获取属性类
       Properties config = new Properties();
       //创建输入流
       InputStream inputStream = new BufferedInputStream(new FileInputStream("G:\\Android_Studio\\AndroidStudioProjects\\JavaCode\\Java\\src\\main\\java\\swu\\xl\\day17_self_properties\\test.xml"));
       //加载xml文件
       config.loadFromXML(inputStream);
       //打印信息
       config.list(System.out);
   }
}

运行结果:
-- listing properties --
password=016891
account=222017602053039

参考文章

  • 通过getResourceAsStream方法获取项目下的指定资源

  • Properties 类的详细使用

你可能感兴趣的:(Java Properties的常见使用)