zoukankan
html css js c++ java
读取src下的属性文件的某个key值
读取src下的属性文件的某个key值
/**
* 读取src下某个属性文件中的某个key所对应的value
*
* @param args
*
*/
public static void main(String[] args) {
String key = "file.upload.folder";
String propertiesFileFullName = "myProperty.properties";
String pbundle = "myProperty";
fun1(propertiesFileFullName, key);
fun2(propertiesFileFullName, key);
fun3(pbundle, key);
}
// 方法一
public static void fun1(String propertiesFileFullName, String key) {
Properties pop = new Properties();
InputStream fs = null;
try {
fs = ReadOneValue.class.getClassLoader().getResourceAsStream(
propertiesFileFullName);
pop.load(fs);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String value = pop.getProperty(key);
System.out.println("属性文件的上传路径为:" + value);
}
// 方法二
public static void fun2(String propertiesFileFullName, String key) {
Properties pop = new Properties();
String classpath = Thread.currentThread().getContextClassLoader()
.getResource("").getPath();
FileInputStream fs = null;
try {
fs = new FileInputStream(classpath + "/" + propertiesFileFullName);
pop.load(fs);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String value = pop.getProperty(key);
System.out.println("读取到的值为:" + value);
}
// 方法三
public static void fun3(String filename, String key) {
Locale locale = Locale.getDefault();
ResourceBundle localResource = ResourceBundle.getBundle(filename,
locale);
String value = localResource.getString(key);
System.out.println("读取到的值为:" + value);
}
}
查看全文
相关阅读:
Java习惯用法总结
为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?
Java 20年:转角遇到Go
史上最全最强SpringMVC详细示例实战教程
即将改变软件开发的5个Java9新特性
<一>c++的编程思路
人生不可破的28个天规
定时任务
redis
mycat分库分表
原文地址:https://www.cnblogs.com/yangy608/p/2726508.html
最新文章
面试题:之字形顺序打印二叉树
面试题:把二叉树打印成多行
面试题:从上往下打印二叉树
面试题:栈的压入。弹出序列
面试题:栈的压入弹出序列
面试题:包含min函数的栈
面试题:对称二叉树
类各成员加载顺序
Java IO-InputStream家族 -装饰者模式
new一个接口
热门文章
lambda表达式
jdk5可变参数列表
ASCII编码
Synchronized关键字
关于Mybatis的几个问题
JDK8新特性:函数式接口
图
读懂Java中的Socket编程
我如何理解Java中抽象类和接口
JSP开发过程遇到的中文乱码问题及解决方案
Copyright © 2011-2022 走看看