直接上代码:

package com.test.test;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle; import org.springframework.core.io.support.PropertiesLoaderUtils; public class TestProperties {
private static TestProperties testProperties = new TestProperties();
public static void main(String[] args) {
//获取properties配置文件中的值
Properties prop = new Properties();
try {
prop.load(test1());//包含2种方法
prop.load(test2());//包含2种方法
prop.load(testProperties.test3());//包含2种方法
//使用spring-core包封装好的方法
prop = PropertiesLoaderUtils.loadAllProperties("test.properties");
Enumeration<?> e = prop.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
System.out.println(key+"="+new String(prop.getProperty(key).getBytes("ISO-8859-1"),"UTF-8"));
}
test4();
test5();
} catch (IOException e) {
e.printStackTrace();
} }
/**
* 使用FileInputStream文件输入流
* @return
*/
public static InputStream test1(){
InputStream in = null;
try {
//此处是相对于项目的相对路径
//in = new FileInputStream("src/main/resources/test.properties");
//或
in = new BufferedInputStream(new FileInputStream("src/main/resources/test.properties"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return in;
}
/**
* 使用ClassLoader
* 默认从classPath路径下找文件
* @return
*/
public static InputStream test2(){
//InputStream in = ClassLoader.getSystemResourceAsStream ("test.properties");
//或
InputStream in = testProperties.getClass().getClassLoader().getResourceAsStream("test.properties");
return in;
}
/**
* 使用class变量的getResourceAsStream()方法
* 文件名前不加“/”,则表示从当前类所在的包下查找该资源
* 文件名前加了“/”,则表示从classPath路径下查找资源
* @return
*/
public InputStream test3(){
//InputStream in = getClass().getResourceAsStream("/test.properties");
//或
InputStream in = TestProperties.class.getResourceAsStream("/test.properties");
return in;
}
/**
* 使用java.util.ResourceBundle类的getBundle()方法
* Locale.getDefault():没有提供语言和地区的资源文件是系统默认的资源文件
* test:不需要文件的后缀
*/
public static void test4(){
try {
ResourceBundle rb = ResourceBundle.getBundle("test", Locale.getDefault());
Enumeration<String> e1 = rb.getKeys();
while (e1.hasMoreElements()) {
String key = e1.nextElement();
System.out.println(key+"="+new String(rb.getString(key).getBytes("ISO-8859-1"),"UTF-8"));
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
* 使用java.util.PropertyResourceBundle类的构造函数
*/
public static void test5(){
InputStream in = ClassLoader.getSystemResourceAsStream ("test.properties");
try {
ResourceBundle rb = new PropertyResourceBundle(in);
Enumeration<String> e1 = rb.getKeys();
while (e1.hasMoreElements()) {
String key = e1.nextElement();
System.out.println(key+"="+new String(rb.getString(key).getBytes("ISO-8859-1"),"UTF-8"));
}
} catch (IOException e) {
e.printStackTrace();
} } }

test.properties文件中的内容是:

name=天若有情
password=天亦老

运行程序后控制台输出test.properties文件中的内容。

java使用java.util.Properties读取properties文件的九种方法的更多相关文章

  1. Properties读取资源文件的四种方法

    package com.action; import java.io.InputStream; import java.util.Locale; import java.util.Properties ...

  2. matlab读取cvs文件的几种方法

    matlab读取CVS文件的几种方法: 1,实用csvread()函数   csvread()函数有三种使用方法: 1.M = csvread('filename')2.M = csvread('fi ...

  3. R语言读取excel文件的3种方法

    R读取excel文件中数据的方法: 电脑有一个excel文件,原始的文件路径是:E:\R workshop\mydata\biom excel数据为5乘2阶矩阵,元素为                ...

  4. Java读取Excel文件的几种方法

    Java读取 Excel 文件的常用开源免费方法有以下几种: 1. JDBC-ODBC Excel Driver 2. jxl.jar 3. jcom.jar 4. poi.jar 简单介绍: 百度文 ...

  5. java 分次读取大文件的三种方法

    1. java 读取大文件的困难 java 读取文件的一般操作是将文件数据全部读取到内存中,然后再对数据进行操作.例如 Path path = Paths.get("file path&qu ...

  6. 读取Excel文件的两种方法

    第一种方法:传统方法,采用OleDB读取EXCEL文件, 优点:写法简单,缺点:服务器必须安有此组件才能用,不推荐使用 private DataSet GetConnect_DataSet2(stri ...

  7. .NET读取Excel文件的三种方法的区别

    ASP.NET读取Excel文件方法一:采用OleDB读取Excel文件: 把Excel文件当做一个数据源来进行数据的读取操作,实例如下: public DataSet ExcelToDS(strin ...

  8. C#读取资源文件的两种方法及保存资源文件到本地

    方法1 GetManifestResourceStream   VB.NET中资源的名称为:项目默认命名空间.资源文件名 C#中则是:项目命名空间.资源文件所在文件夹名.资源文件名 例如:istr = ...

  9. PHP读取大文件的几种方法介绍

    读取大文件一直是一个头痛的问题,我们像使用php开发读取小文件可以直接使用各种函数实现,但一到大文章就会发现常用的方法是无法正常使用或时间太长太卡了,下面我们就一起来看看关于php读取大文件问题解决办 ...

随机推荐

  1. IOS MapKit框架的使用(专门用于地图显示)

    ● MapKit框架使用前提 ● 导入框架 ● 导入主头文件#import <MapKit/MapKit.h>   ●  MapKit框架使用须知 ●  MapKit框架中所有数据类型的前 ...

  2. hdu-2197 本原串---枚举因子+容斥定理

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2197 题目大意: 由0和1组成的串中,不能表示为由几个相同的较小的串连接成的串,称为本原串,有多少个 ...

  3. 【[CTSC2018]混合果汁】

    题目 \(CTSC\)也有这么水的题啊 首先看到美味值来自于最小的美味值,我们就可以先考虑把所有的果汁按照美味值排序 接下来可以考虑二分,二分出一个\(mid\)我们只使用美味值大于等于\(mid\) ...

  4. PyCharm Notes | PyCharm 使用笔记(远程访问服务器code配置指南)

    PyCharm is a strong IDE for python programmer. Not only because it has a similar face with VS or som ...

  5. ids for this class must be manually assigned before calling save():

    Caused by: javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: i ...

  6. Storm 中drpc调用

    package storm.starter; import backtype.storm.Config; import backtype.storm.LocalCluster; import back ...

  7. 对象的比较与排序(三):实现IComparable<T>和IComparer<T>泛型接口

    来源:http://www.cnblogs.com/eagle1986/archive/2011/12/06/2278531.html 1:比较和排序的概念 比较:两个实体类之间按>,=,< ...

  8. [USACO08NOV]时间管理Time Management(排序,贪心)

    题目描述 作为一名忙碌的商人,约翰知道必须高效地安排他的时间.他有N工作要 做,比如给奶牛挤奶,清洗牛棚,修理栅栏之类的. 为了高效,列出了所有工作的清单.第i分工作需要T_i单位的时间来完成,而 且 ...

  9. Python常用的数据类型

    Python常用的数据类型有很多,今天暂时介绍这三种,int(整数类型).str(字符串).bool(布尔类型)一.int(整数类型)1.不带小数的,integer 的缩写,常用于数据的计算或者大小的 ...

  10. Mac mysql安装失败解决方法

    在mac终端通过命令安装mysql,提示错误,解决方法如下: (1)安装命令:brew install mysql (2)提示错误: Error:Could not create /usr/local ...