java加载properties文件的六种方法总结

java加载properties文件的六中基本方式实现

java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载;

另一种是通过import java.util.ResourceBundle类的getBundle(String baseName)方法加载。

注意:一定要区分路径格式

实现代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.util;
 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
 
public class PropertiesUtil {
  private static String basePath = "src/prop.properties";
  private static String name = "";
  private static String nickname = "";
  private static String password = "";
 
  /**
   * 一、 使用java.util.Properties类的load(InputStream in)方法加载properties文件
   *
   */
  public static String getName1() {
    try {
      Properties prop = new Properties();
      InputStream is = new FileInputStream(basePath);
      prop.load(is);
      name = prop.getProperty("username");
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return name;
  }
 
  /**
   * 二、 使用class变量的getResourceAsStream()方法
   * 注意:getResourceAsStream()读取路径是与本类的同一包下
   *
   */
  public static String getName2() {
    Properties prop = new Properties();
    InputStream is = PropertiesUtil.class
        .getResourceAsStream("/com/util/prop.properties");
    try {
      prop.load(is);
      name = prop.getProperty("username");
    } catch (IOException e) {
      e.printStackTrace();
    }
    return name;
  }
 
  /**
   * 三、
   * 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
   * getResourceAsStream(name)方法的参数必须是包路径+文件名+.后缀 否则会报空指针异常
   *
   */
  public static String getName3() {
    Properties prop = new Properties();
    InputStream is = PropertiesUtil.class.getClassLoader()
        .getResourceAsStream("com/util/prop.properties");
    try {
      prop.load(is);
 
    } catch (IOException e) {
      e.printStackTrace();
    }
    return name;
  }
 
  /**
   * 四、 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
   * getSystemResourceAsStream()方法的参数格式也是有固定要求的
   *
   */
  public static String getName4() {
    Properties prop = new Properties();
    InputStream is = ClassLoader
        .getSystemResourceAsStream("com/util/prop.properties");
    try {
      prop.load(is);
      name = prop.getProperty("username");
    } catch (IOException e) {
      e.printStackTrace();
    }
    return name;
  }
 
  /**
   * 五、 使用java.util.ResourceBundle类的getBundle()方法
   * 注意:这个getBundle()方法的参数只能写成包路径+properties文件名,否则将抛异常
   *
   */
  public static String getName5() {
    ResourceBundle rb = ResourceBundle.getBundle("com/util/prop");
    password = rb.getString("password");
    return password;
  }
 
  /**
   * 六、 使用java.util.PropertyResourceBundle类的构造函数
   *
   */
  public static String getName6() {
    try {
      InputStream is = new FileInputStream(basePath);
      ResourceBundle rb = new PropertyResourceBundle(is);
      nickname = rb.getString("nickname");
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
 
    return nickname;
  }
 
  /**
   * 测试
   *
   */
  public static void main(String[] args) {
    System.out.println("name1:" + PropertiesUtil.getName1());
    System.out.println("name2:" + PropertiesUtil.getName2());
    System.out.println("name3:" + PropertiesUtil.getName3());
    System.out.println("name4:" + PropertiesUtil.getName4());
    System.out.println("password:" + PropertiesUtil.getName5());
    System.out.println("nickname:" + PropertiesUtil.getName6());
  }
}

java加载properties文件的六种方法总结的更多相关文章

  1. java加载properties文件的六中基本方式实现

    java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载: 另一种是通过impo ...

  2. Java加载资源文件几种方法

    from: http://andyzhu.blog.51cto.com/4386758/775836/ import java.net.URL; import org.springframework. ...

  3. Java加载资源文件的两种方法

    处理配置文件对于Java程序员来说再常见不过了,不管是Servlet,Spring,抑或是Structs,都需要与配置文件打交道.Java将配置文件当作一种资源(resource)来处理,并且提供了两 ...

  4. 加载properties文件的三种方法

    源代码: package a.one; import java.io.FileInputStream; import java.io.InputStream; import java.util.Pro ...

  5. JAVA加载Properties配置资源文件

    JAVA加载Properties配置资源文件 制作人:全心全意 配置文件(资源文件):以properties作为拓展名的文件 Java代码是如何加载properties文件的? 必须使用Propert ...

  6. xml文件 加载properties文件的两种方法与注意事项

    1.遇到的问题: 配置redisSpringContext.xml 时,遇到 properties加载失败,提示BeanDefinitionStoreException  和   java.lang. ...

  7. Java读取Properties文件的六种方法

    使用J2SE API读取Properties文件的六种方法 1.使用java.util.Properties类的load()方法示例: InputStream in = lnew BufferedIn ...

  8. Java开发学习(八)----IOC/DI配置管理第三方bean、加载properties文件

    前面的博客都是基于我们自己写的类,现在如果有需求让我们去管理第三方jar包中的类,该如何管理? 一.案例:数据源对象管理 本次案例将使用数据源Druid和C3P0来配置学习下. 1.1 环境准备 学习 ...

  9. spring入门(二)【加载properties文件】

    在开发过程当中需要用到配置信息,这些信息不能进行硬编码,这时配置文件是一个比较好的方式,java提供了properties格式的文件,以键值对的方式保存信息,在读取的时候通过键获得键对应的值,spri ...

随机推荐

  1. Android系统定制----删除系统锁屏功能【转】

    本文转载自:http://blog.csdn.net/morixinguan/article/details/56675914 frameworks/base/packages/SettingsPro ...

  2. POJ 2080:Calendar

    Calendar Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12546   Accepted: 4547 Descrip ...

  3. pandas 绘图 机器学习看特征相关性

    pandas 绘图 import numpy as np import tflearn from tflearn.layers.core import dropout from tflearn.lay ...

  4. c# IP从192.168.1.1转成int类型

    找了一些资料,总结如下: 方法1 .net提供的方法转换IP地址 //字符串转换为数字 System.Net.IPAddress ipaddress = System.Net.IPAddress.Pa ...

  5. cmd 高级用法

    1. 查看服务(service)信息 查看所有启动的服务信息: C:\Users\hasee>net start 根据启动的服务名,进一步对其启动和关闭: C:\Users\hasee>n ...

  6. 21 WPF数据视图

    视图对象 当你绑定集合到ItemsControl,在幕后数据视图被安静地创造.视图位于数据源和绑定控件之间.数据视图是通往数据源的一个窗口.它跟踪当前项目,它支持诸如排序,过滤,和分组特征.这些特征独 ...

  7. 【高德地图API】地理编码与逆地理编码

    一.地理编码 该功能实现地理编码服务,即地址匹配,从已知的地址描述到对应的经纬度坐标的转换,即根据地址信息,查询该地址所对应的点坐标等,地址(address) 为必选项,城市(city)为可选项. & ...

  8. 31. ExtJs4回车事件监听

    转自:https://710542316.iteye.com/blog/2148542 监听表单字段事件: Ext使得对用户某个动作的监听特别简单,诸如单击某个元素或者按下某个键盘上的键. 一个经常性 ...

  9. Feature分支(转载)

    转自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137602623300 ...

  10. Tomcat的jvm配置

    Tomcat本身不能直接在计算机上运行,需要依赖于操作系统和一个JAVA虚拟机.Tomcat的内存溢出本质就是JVM内存溢出,JAVA程序启动时JVM会分配一个初始内存和最大内存给程序.当程序需要的内 ...