随笔 - 832  文章 - 2  评论 - 31  阅读 - 167万

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

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

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

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

 

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

 

实现代码如下:

复制代码
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());
  }
}
复制代码

文件路径:

 

 prop.properties文件:

username=mamama
nickname=xiaoma
password=123456

输出结果:

 

 

posted on   小破孩楼主  阅读(1468)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示