【Properties文件】Java使用Properties来读取配置文件
配置文件位置及内容







执行结果

程序代码
package Utils.ConfigFile;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
public class PropertiesTest {
/**
* 获取属性文件
* @param path 属性文件路径
* @return
*/
public static Properties getPropsFile(String path) {
Properties props = new Properties();
try {
File file = new File(path);
InputStream in = new BufferedInputStream(new FileInputStream(file));
//解决中午乱码问题--因为字节流无法读取中文,所以采用reader把inputStream转换成reader用字符流来读取中文
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
props.load(bf);
in.close();
} catch (Exception e) {
return null;
}
return props;
}
/**
* 显示所有键值
* @param properties
*/
public static void showKeys(Properties properties){
Enumeration<?> enumeration = properties.propertyNames();
System.out.println("======下面将显示所有key值============");
while(enumeration.hasMoreElements()){
Object key = enumeration.nextElement();
System.out.println(key);
}
}
/**
* 显示所有value值
* @param properties
*/
public static void showValues(Properties properties){
Enumeration<?> enumeration = properties.elements();
System.out.println("======下面将显示所有value值============");
while(enumeration.hasMoreElements()){
Object value = enumeration.nextElement();
System.out.println(value);
}
}
/**
* 显示所有key,value
* @param properties
*/
public static void showKeysAndValues(Properties properties){
Iterator<Map.Entry<Object, Object>> it= properties.entrySet().iterator();
System.out.println("======下面将显示所有<key,value>值---方式1============");
while (it.hasNext()) {
Map.Entry<Object, Object> entry = it.next();
Object key = entry.getKey().toString();
Object value = entry.getValue();
System.out.println("<" + key + "," + value + ">");
}
}
/**
* 显示所有key,value
* @param properties
*/
public static void showKeysAndValues2(Properties properties){
System.out.println("======下面将显示所有<key,value>值--方式2============");
for (Map.Entry<Object, Object> entry: properties.entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println("<" + key + "," + value + ">");
}
}
public static void main(String args[]) {
Properties propFile = getPropsFile("C:\\myProperties.properties");
showKeys(propFile);
showValues(propFile);
showKeysAndValues(propFile);
showKeysAndValues2(propFile);
}
}
【Properties文件】Java使用Properties来读取配置文件的更多相关文章
- JAVA使用相对路径读取配置文件
JAVA使用相对路径读取配置文件[align=center][/align][size=medium][/size] 在软件开发中经常遇到读取配置文件,以及文件定位问题.今天做个总结. (一) ...
- 读取properties文件------servletcontext及dao层读取
用servletcontext读取properties文件-------1) 重点在于:InputStream in=this.getServletContext().getResourceAsStr ...
- 利用Properties属性集结合类加载器读取配置文件
配置文件test.properties a=123 测试类Demo1.java public class Demo1 { public static void main(String[] args) ...
- 如何读取jar包外的properties文件和log4j.properties
http://jrails.iteye.com/blog/1705464 ***************************************' 一般在项目中使用properties配置文件 ...
- 将properties文件放在Jar包并读取
有时候需要在一个library内部打包一个properties文件,包含一些配置信息,而不能部署在外部. 在maven工程里面,将properties文件放在src/main/resources目录下 ...
- Java工程中如何读取配置文件中参数信息
Java中读取配置文件中参数: 方法一:通过JDK中Properties来实现对配置文件的读取. Properties主要用于读取Java的配置文件,不同的编程语言有自己所支持的配置文件,配置文件中很 ...
- java 4种方式读取配置文件 + 修改配置文件
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 方式一采用ServletContext读取读取配置文件的realpath然后通过文件流读取出来 方式二采用ResourceB ...
- java读取package中的properties文件java.util.MissingResourceException
文件结构: /build/classes/d914/Hello.class /build/classes/d914/mess.properties /build/classes/d914/mess_z ...
- Java读取Properties文件 Java加载配置Properties文件
static{ Properties prop = new Properties(); prop.load(Thread.currentThread().getContextClassLoader() ...
- java web编程 servlet读取配置文件参数
新建一个servlet. 然后在web.xml文件里面自动帮助你创建好了<servlet-name><servlet-class><servlet-mapping> ...
随机推荐
- 关于KVM的几篇细节文档
1. Qemu Study http://lists.gnu.org/archive/html/qemu-devel/2011-04/pdfhC5rVdz7U8.pdf http://handboo ...
- Activity Lifecycle
Activity Lifecycle Activities in the system are managed as an activity stack(activity栈?). When a new ...
- python 的一些小技巧
赋值: a, b, c = 'xixi', 'haha', 'hehe' 连接字典: >>> s = {1:'a', 2:'b', 3:'c'} >>> s.key ...
- UVa 11039 - Building designing
题目大意:n个绝对值各不相同的非0整数,选出尽量多的数,排成一个序列,使得正负号交替且绝对值递增. 分析:按照绝对值大小排一次序,然后扫描一次,顺便做个标记即可. #include<cstdio ...
- this指针基础介绍
=================this指针的由来==================== 一个学生可以有多本书一样,而这些书都是属于这个同学的:同理,如果有很多个同学在一起,那么为了确定他们的书不 ...
- ListView滚动到顶部
videoAdapter.notifyDataSetChanged();videoListView.setSelection(0); 注意顺序先notify后setSelection
- Spring事务配置的五种方式 -- 越往后需要Spring版本越高
第五种 基本零配置 个人感觉第四种也可以 Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式, ...
- UVa 699 下落的树叶
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- leetcode 98 Validate Binary Search Tree ----- java
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- codeforces 725/C
Hidden Word time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...