第一种:通过Properties读取

package study.utils;
 
import java.io.IOException;
import java.util.Properties;
 
public class PropertyUntil{
    private static Properties properties;
    
    private static void setProperty(){
        if (properties==null) {
            properties = new Properties();
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            try {
                properties.load(loader.getResourceAsStream("test.properties"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static String getPropertiesKey(String key){
        if (properties==null) {
            setProperty();
        }
        return properties.getProperty(key, "default");
    }
}

 public static void main(String[] args) {
        System.out.println(getPropertiesKey("test"));
    }

 

第二种:通过ResourceBundle来读取

public static String test = "";

    static {
        ResourceBundle bundle = ResourceBundle.getBundle("test");//配置文件名称,不能带后缀文件类型
        test = bundle.getString("test");//配置文件中的key=value
    }

    public static void main(String[] args) {
        System.out.println(test);
    }

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐