1、工程结构

2、ConfigFileTest.java

package com.configfile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties; public class ConfigFileTest { private static Properties config = null; static {
InputStream in = ConfigFileTest.class.getClassLoader().getResourceAsStream("parameter.properties");
config = new Properties();
try {
config.load(in);
in.close();
} catch (IOException e) {
//读取url.properties出错
e.printStackTrace();
}
} /**
* 通过键获得对应的值
* @param key
* @return
*/
public static String getValue(String key) {
try {
String value = config.getProperty(key);
return value.trim();
} catch (Exception e) {
e.printStackTrace();
return null;
}
} /**
* 读取properties的全部信息
*/
public static void getAllProperties() {
try {
Enumeration en = config.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = config.getProperty(key);
System.out.println("key = "+config.getProperty(key));
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("ConfigInfoError" + e.toString());
}
}
public static void main(String args[]) {
System.out.println(getValue("title"));
getAllProperties();
}
}

注:1、properties类详解见:Java中Properties类的操作JAVA操作properties文件

  2、getClass():取得当前对象所属的Class对象   
       getClassLoader():取得该Class对象的类装载器
       类装载器负责从Java字符文件将字符流读入内存,并构造Class类对象,在你说的问题哪里,通过它可以得到一个文件的输入流
3、parameter.properties

title=\u4E2D\u56FD\u4F60\u597D ##爱你中国
message=\u6211\u6765\u4E86 ##我来了

Java之properties文件读取的更多相关文章

  1. Java的properties文件读取和属性修改

    package Test; import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileO ...

  2. Java学习-019-Properties 文件读取实例源代码

    在这几天的学习过程中,有开发的朋友告知我,每个编程语言基本都有相应的配置文件支持类,像 Python 编程语言中支持的 ini 文件及其对应的配置文件读取类 ConfigParse,通过这个类,用户可 ...

  3. Java配置文件Properties的读取、写入与更新操作

    /** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...

  4. JAVA操作properties文件

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

  5. 对Java配置文件Properties的读取、写入与更新操作

    http://breezylee.iteye.com/blog/1340868 对Java配置文件Properties的读取.写入与更新操作 博客分类: javase properties  对Jav ...

  6. 实现对Java配置文件Properties的读取、写入与更新操作

    /** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...

  7. properties文件读取

    package properties; import java.io.FileInputStream; import java.io.FileNotFoundException; import jav ...

  8. Java读properties文件中文乱码问题的解决方法

    java读properties文件,包含中文字符的主要有两种: 1.key中包含中文字符的(value中也有可能包含) 2.key中不包含中文字符的(value中有可能包含) 1.key中包含中文字符 ...

  9. java读properties文件 乱码

    java读properties文件,包含中文字符的主要有两种: 1.key中包含中文字符的(value中也有可能包含) 2.key中不包含中文字符的(value中有可能包含) 1.key中包含中文字符 ...

随机推荐

  1. MooseFs-分布式文件系统系列(二)之安装总结

    preface 上篇博客写了如何安装MFS,那么现在就说说经验总结把 安装方式有源码和yum安装,在官网地址:https://moosefs.com/download/centosfedorarhel ...

  2. vim配置有竖对齐线

    https://github.com/lvxiaobo616/vim-indent-guides 参考 https://github.com/Yggdroot/indentLine 先安装 Yggdr ...

  3. python 基于windows环境的ftp功能

    描述: 1.基于备份服务器部署的py程序,将需要备份主机目录下的内容下载至备份服务器(服务端和远端都是windows server 2008) 2.py程序部署在windows服务器,后台运行,基于b ...

  4. 【转】Yeoman自动构建 Angularjs 项目

    Yeoman是什么? Yeoman按照官方说法,它不只是一个工具,还是一个工作流.它其实包括了三个部分yo.grunt.bower,分别用于项目的启动.文件操作.包管理. Yo: Yo是一个项目初始化 ...

  5. emacs最简单入门,只要10分钟

    macs最简单入门,只要10分钟  windwiny @2013    无聊的时候又看到鼓吹emacs的文章,以前也有几次想尝试,结果都是玩不到10分钟就退出删除了. 这次硬着头皮,打开几篇文章都看完 ...

  6. magelinux notes

    [root@centos01 01]# cd ~jack #进入指定用户的家目录[root@centos01 jack]# cd - #切回上一次目录[root@centos01 home]# cd ...

  7. Random类

    Random类是随机数产生类,可以指定一个随机数的范围,然后任意产生在此范围中的数字. //================================================= // F ...

  8. 9-slice-scaling

    9-slice-scaling http://rwillustrator.blogspot.com/2007/04/understanding-9-slice-scaling.html

  9. yourphp的edit,updata,dele

    参考文件Yourphp\Lib\Action\User\PostAction.class.php public function add() { $form=new Form(); $form-> ...

  10. js与php转换时间戳

    php时间:1368524732 js代码: function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleStri ...