直接上代码:

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. SQL Server的Bug

    SQL 语句如下: SELECT * FROM Production.Product WHERE ProductNumber IN(SELECT ProductNumber FROM HumanRes ...

  2. MySQL优化 ----开篇

    今天,数据库的操作越来越成为整个应用的性能瓶颈,Mysql优化则是一个经常要谈的问题了. 谈起MySQL优化,咱们先简单谈一下Mysql: Mysql是最流行的关系型数据库管理系统,在WEB应用方面M ...

  3. mybais学习记录一——入门程序

    一.传统连接数据库和执行sql的不足 1.数据库连接,使用时就创建,不使用立即释放,对数据库进行频繁连接开启和关闭,造成数据库资源浪费,影响 数据库性能. 设想:使用数据库连接池管理数据库连接. 2. ...

  4. HDU 1160(两个值的LIS,需dfs输出路径)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1160 FatMouse's Speed Time Limit: 2000/1000 MS (Java/ ...

  5. deep learning学习记录二

    接着我的微博继续八卦吧 微博里问了几个人,关于deep learning和cnn的区别,有不少热心网友给了回答,非常感谢.结合我听课和看文章的理解,我大胆大概总结一下: 在上世纪90年代,neural ...

  6. Python基础—01-认识python,编写第一个程序

    认识python 发展历史:点此查看简介 就业方向: WEB.爬虫.运维.数据分析.机器学习.人工智能.... 版本选择 python2.7是最后一个py2的版本,2020年将不再提供支持 pytho ...

  7. Vue组件:组件的动态添加与删除

    一.实现效果 二.实现代码 HelloWorld.vue <template> <div class="hello"> <child-page v-f ...

  8. php第一节(入门语法、数据类型)

    <?php /** * 变量命名用 $ 符 * 变量名称的命名规范 * 1.变量名称以$标示 * 2.变量名称只能以字母和下划线开头 * 3.变量的名称只能包含字母.下划线.数字 * 4.变量名 ...

  9. #leetcode刷题之路2-两数相加

    给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...

  10. 【TOJ 1912】487-3279(hash+map)

    描述 Businesses like to have memorable telephone numbers. One way to make a telephone number memorable ...