【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> ...
随机推荐
- Kali 找回root 密码的操作步骤
1. 重启kali 进入grub 界面,选择 “kali GNU/Linux, Linux 3.7-trunk-686-pae(恢复模式)” 2. 然后按下键盘E 键 3.进入编辑模式,找到Linux ...
- iOS学习笔记---oc语言第七天
类的扩展 NSDate是Cocoa中用于处理日期和时间的基础类,封装了某一给定的时刻,具体的日期 时间和时区 使用+date方法获取当前日期和时间 NSDate *date = [NSDate dat ...
- HQL查询语言的使用介绍
@SuppressWarnings("deprecation") public class HibernateUtil { private static final Session ...
- Android拍照保存图片内存大小
图片拍摄的大小会随着硬件而变化,比如,像素高的相机拍出来的图片要比像素低的图片内存要大. 如此一来,针对机型可能调用camera app保存照片的时候,图片大小会不一样. 为了缩小图片大小,我们需要把 ...
- jquery的ajax异步请求接收返回json数据
http://www.jb51.net/article/51122.htm jquery的ajax异步请求接收返回json数据方法设置简单,一个是服务器处理程序是返回json数据,另一种就是ajax发 ...
- leetcode 139. Word Break ----- java
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- Linux驱动设计—— 驱动调试技术
参考博客与书籍: <Linux设备驱动开发详解> <Linux设备驱动程序> http://blog.chinaunix.net/uid-24219701-id-2884942 ...
- 【转】 iOS日常学习 - iOS10上关于NSPhotoLibraryUsageDescription等问题
原文网址:http://blog.csdn.net/wang631106979/article/details/52578001 最近升级了Xcode8.0,真是很多坑啊,填完一个来另外一个,今天又遇 ...
- excel动态去重和动态排序
其实去重和排序的方法很多,没有哪一种更好,实时去重总会省一些时间,刚好也练习了下数组公式 动态去重: =IF(ROW()<=COUNTA(员工基础数据!H:H),INDEX(员工基础数据!H:H ...
- Abdicate
每次,打开背单词软件,背诵单词时,A打头的单词,第一个,永远会是 Abdicate,放弃,抛弃. 真是有意思. 想想在学习js高级程序设计一书时,一横心,决心手动输入书中出现过的每一个代码片段示例. ...