直接上代码:

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. 每天一个linux命令(22):tar命令

    通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar命令可以为linux ...

  2. Android(java)学习笔记36:Scanner类使用

    1. Scanner类使用 package cn.itcast_01; /* * Scanner:用于接收键盘录入数据. * * 前面的时候: * A:导包 * B:创建对象 * C:调用方法 * * ...

  3. heidsql(mysql)安装教程和mysql修改密码

    简单介绍安装 官网下载:https://mariadb.org/download/ 直接下载(mariadb-10.3.9-winx64.msi):https://github.com/weibang ...

  4. 188. Best Time to Buy and Sell Stock IV——LeetCode

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. Redis-cluster详解

    redis集群结构 特点:     1 所有redis节点(包括主和从)彼此互联(两两通信),底层使用内部的二进制传输协议,优化传输速度;(所有功能特点的基础)    2 集群中也有主从,也有高可用的 ...

  6. Golang学习笔记(一)

    一段基础的go语言代码解析 package main import "fmt" func main(){ fmt.Println("hello golang") ...

  7. plupload批量上传分片(后台代码)

    plupload批量上传分片功能, 对于文件比较大的情况下,plupload支持分片上传,后台代码如下: /** * * 方法:upLoadSpecialProgramPictrue * 方法说明:本 ...

  8. 5820. 【NOIP提高A组模拟2018.8.16】 非法输入(模拟,字符串)

    5820. [NOIP提高A组模拟2018.8.16] 非法输入 (File IO): input:aplusb.in output:aplusb.out Time Limits: 1000 ms   ...

  9. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--I-填空题

    链接:https://www.nowcoder.com/acm/contest/90/I 来源:牛客网 1.题目描述 牛客网是是一个专注于程序员的学习和成长的专业平台,集笔面试系统.课程教育.社群交流 ...

  10. linux下mysql主从复制,实现数据库同步

    运行环境: 查看linux版本命令:lsb_release -a 主服务器:centos release 6.5 mysql 5.6.10-log  IP:172.17.200.25从服务器:cent ...