今天为了通过java读取properties文件,google了很长时间,终于找到了。现在特记录之和大家一起分享。 
    下面直接贴出代码:java类

public class Mytest
public static void readFile(String fileName) {//传入参数fileName是要读取的资源文件的文件名如(file.properties)
InputStream in = null;
Properties pros = new Properties();
try {
if (null != fileName) {
//前提是资源文件必须和Mytest类在同一个包下
in = Mytest.class.getResourceAsStream(fileName);
//得到当前类的路径,并把资源文件名作为输入流
pros.load(in);
Enumeration en = pros.propertyNames();//得到资源文件中的所有key值
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
System.out.println("key=" + key + " value=" + pros.getProperty(key));
//输出资源文件中的key与value值
}
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("读取资源文件出错");
} finally {
try {
if (null != in) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭流失败");
}
} }

方法二:

import java.util.MissingResourceException;
import java.util.ResourceBundle; public class Messages {
private static final String BUNDLE_NAME = "com.xxx.cs.mm.service.messages"; //messages.properties文件和Messages类在同一个包下,包名:com.xxx.cs.mm.service private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME); private Messages() {
} public static String getString(String key) {
try {
return RESOURCE_BUNDLE.getString(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}
}

转自:http://duqiangcise.iteye.com/blog/319793

使用J2SE API读取Properties文件的六种方法

1。使用java.util.Properties类的load()方法
示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);

2。使用java.util.ResourceBundle类的getBundle()方法
示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

3。使用java.util.PropertyResourceBundle类的构造函数
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);

4。使用class变量的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例: InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);

补充

Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);

注意:

this.getclass.getResourceAsStream(name);  //类与资源文件同级--同一个目录下

this.class.getClassLoader().getResourceAsStream(name); //资源文件与classpath同级。

未知来源

用java读取properties文件--转的更多相关文章

  1. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  2. java 读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  3. java基础学习总结——java读取properties文件总结

    摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...

  4. java读取properties文件时候要注意的地方

    java读取properties文件时,一定要注意properties里面后面出现的空格! 比如:filepath = /home/cps/ 我找了半天,系统一直提示,没有这个路径,可是确实是存在的, ...

  5. java基础—java读取properties文件

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  6. Java基础学习总结(15)——java读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  7. java读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  8. java读取.properties文件

    在web开发过程中,有些配置要保存到properties文件里,本章将给出一个工具类,用来方便读取properties文件. 案例: 1:config.properties文件 name=\u843D ...

  9. Java 读取Properties文件时应注意的路径问题

    1. 使用Class的getResourceAsStream()方法读取Properties文件(资源文件)的路径问题:  InputStream in = this.getClass().getRe ...

随机推荐

  1. JAVA 抛出与声明异常

    在编程过程中,我们往往会遇到这种情况,在当前环境中无法解决,比如用户传入的参数错误,IO设备问题等.此时,就要从当前环境中抛出异常提交给上级来处理. 在JAVA语言中,使用throw关键字来抛出异常. ...

  2. show processlist 输出ID 和 information_schema.PROCESSLIST 的id,information_schema.innodb_trx的TRX_MYSQL_T

    Session 1: mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> update Client ...

  3. ☀【JS组织】pageA_init, pageB_init

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  4. Reason: Server is in single user mode. Only one administrator can connect at this time

    单击Start→All Programs→Microsoft SQL Server 2008→Configuration Tools→SQL Server Configuration Manager. ...

  5. LoadRunner_Analysis(z) 分析

    LoadRunner_Analysis(z) 分析 lr_Analysis(z) Analysis Summary Page Analysis Summary(分析总结页面) 分为三个部分: Stat ...

  6. suse系统FTP问题

    1.修改FTP端口问题 在 这个配置文件中vi /etc/vsftpd.conf 添加 Listen_port=34 vi /etc/services ftp        21/tcp    # F ...

  7. Bzoj 1726: [Usaco2006 Nov]Roadblocks第二短路 dijkstra,堆,A*,次短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 969  Solved: 468[S ...

  8. Bzoj 3173: [Tjoi2013]最长上升子序列 平衡树,Treap,二分,树的序遍历

    3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1183  Solved: 610[Submit][St ...

  9. 开发小技巧:C#逐个输出字符

    静态自定义方法: static int counter = 0; static string displayString = "This string will appear one let ...

  10. ios 中的半屏幕底部弹出框

    static UIView *modalView;if (modalView) { [modalView removeFromSuperview]; modalView = nil; return; ...