【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> ...
随机推荐
- python报错
报错1 UnboundLocalError: local variable 'x' referenced before assignment 定义了一个全局参数,但是在函数中直接改变参数值,就会报这个 ...
- ajax简单后台交互-我们到底能走多远系列(28)
我们到底能走多远系列(28) 1,扯淡 单身的生活,大部分时间享受自由,小部分时间忍受寂寞. 生活有时候,其实蛮苦涩,让人难以下咽.那些用岁月积累起来的苦闷,无处宣泄,在自己的脑海里蔓延成一片片荆棘, ...
- mysql保存中文乱码的原因和解决办法
当你遇到这个mysql保存中文乱码问题的时候,期待找到mysql保存中文乱码的原因和解决办法这样一篇能解决问题的文章是多么激动人心. 也许30%的程序员会选择自己百度,结果发现网友已经贴了很多类 ...
- CentOS下解决”用户账户is not in the sudoers file“问题
如上图,在当前用户cent(我的用户名)下使用sudo命令时,提示"cent is not in the sudoers file. This incident will be report ...
- 331. Verify Preorder Serialization of a Binary Tree
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...
- char类型与Unicode的编码
Java的char型是非常独特的,占用两个字节,因为Java中char型采用了Unicode编码. 要理解这个问题,我们必须要理解什么是Unicode. 世界上存在着多种编码方式,同一个二进制数字可以 ...
- 《C标准库》——之<ctype.h>
在没读<ctype.h>的源码之前,我一直以为我们平时用的isalnum.isdigit.isalpha等这些函数,是靠判断写出来的. 比如: int isdigit(int c){ re ...
- yum命令
1)查询 yum list #查询所有可用软件包列表 yum search 关键字 #搜索服务器上和关键字相关的包 #对比rpm的查询:rpm -qa | grep xxx 2)安装 yum ...
- typedef 和 define的区别
类型取别名,还可以定义常量.变量.编译开关 都知道两个在某些情况下是相同的 但是define是在预编译时就会处理掉,进行简单的宏替换,不管正不正确都替换掉,末尾没有分号,有分号连分号也一起替换了. 而 ...
- kuangbin_UnionFind A (POJ 2236)
挺接近模板的一题 接受O操作的时候扫一遍 符合条件的merge进去 done #include <cstdio> #include <cstring> #include < ...