不让用常量类,那就用.properties文件配置,放在根目录。

 import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties; /**
* Created by syl on 2017/12/12 0007. 读取常量properties文件
*/
public class PropertyUtil {
@SuppressWarnings("rawtypes")
private static Map map = null; @SuppressWarnings({ "rawtypes", "unchecked" })
private static void loadFile() {
map = new HashMap();
try {
Properties p = new Properties();
p.load(PropertyUtil.class.getClassLoader().getResourceAsStream("params.properties"));
Iterator it = p.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
String value = p.getProperty(key);
map.put(key, value);
}
} catch (Exception e) {
e.printStackTrace();
}
} public static String getValue(String str) {
if (map == null) {
loadFile();
}
return (String) map.get(str);
} public static void main(String[] args) {
String s = PropertyUtil.getValue("test");
System.out.println(s);
}
}

控制台输出:

java 配置在.properties文件中的常量的更多相关文章

  1. SpringMVC+HibernateValidator,配置在properties文件中的错误信息回显前端页面出现中文乱码

    问题: 后台在springMVC中使用hibernate-validator做参数校验的时候(validator具体使用方法见GOOGLE),用properties文件配置了校验失败的错误信息.发现回 ...

  2. java读取properties文件中参数值

    在类文件中加入代码: //config.properties.sysInfo //sysInfo.properties在文件夹的路径为/src/config/properties/sysInfo.pr ...

  3. springboot使用@Value注入properties文件中的值,中文乱码

    最近开发一个需求,讲一个中文值配置在properties文件中,然后代码中使用@Value注解进行注入使用,然而出现了如下状况: 中文出现乱码,将代码修改如下: String str = new St ...

  4. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  5. Java之properties文件读取

    1.工程结构 2.ConfigFileTest.java package com.configfile; import java.io.IOException; import java.io.Inpu ...

  6. 用java读取properties文件--转

    今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享.     下面直接贴出代码:java类 public class Mytest pub ...

  7. JAVA操作properties文件

    va中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properties ...

  8. java 读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  9. java基础学习总结——java读取properties文件总结

    摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...

随机推荐

  1. sonar安装问题记录

    1.启动时日志中提示 Caused by: java.lang.RuntimeException: can not run elasticsearch as root 错误原因:因为安全问题elast ...

  2. ArrayList用法详解与源码分析

    说明 此文章分两部分,1.ArrayList用法.2.源码分析.先用法后分析是为了以后忘了查阅起来方便-- ArrayList 基本用法 1.创建ArrayList对象 //创建默认容量的数组列表(默 ...

  3. 广告小程序后端开发(2.Models设计)

    1.users的数据表设计: 1.重建用户表: 1.在settings中配置: AUTH_USER_MODEL='users.UserProfile' 2.apps/users/models.py中建 ...

  4. Hierarchical Z-Buffer Occlusion Culling

    While I was at GDC I had the pleasure of attending the Rendering with Conviction talk by Stephen Hil ...

  5. 526. Beautiful Arrangement

    Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...

  6. css3边角旋转

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 牛客挑战赛30D 小A的昆特牌(组合数学)

    题面 传送门 题解 很容易写出一个暴力 \[\sum_{i=l}^r {i+n-1\choose n-1}{s-i+m\choose m}\] 即枚举选了多少个步兵,然后用插板法算出方案数 我们对这个 ...

  8. Linux下对于makefile的理解

    什么是makefile呢?在Linux下makefile我们可以把理解为工程的编译规则.一个工程中源文件不计数,其按类型.功能.模块分别放在若干个目录中,makefile定义了一系列的规则来指定,那些 ...

  9. 循环神经网络中BFTT的公式推导

    一.变量定义 此文是我学习BFTT算法的笔记,参考了雷明<机器学习与应用>中的BFTT算法推导,将该本书若干个推导串联起来,下列所有公式都是结合书和资料,手动在PPT上码的,很费时间,但是 ...

  10. Git的一些用法(下)

    (4) 提交分支 提交分支命令 : 将本地的分支提交到 GitHub中; git push origin experiment (5) 分支合并移除 合并分支命令 : 合并分支之后, 分支中有的文件在 ...