java使用java.util.Properties读取properties文件的九种方法
直接上代码:
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文件的九种方法的更多相关文章
- Properties读取资源文件的四种方法
package com.action; import java.io.InputStream; import java.util.Locale; import java.util.Properties ...
- matlab读取cvs文件的几种方法
matlab读取CVS文件的几种方法: 1,实用csvread()函数 csvread()函数有三种使用方法: 1.M = csvread('filename')2.M = csvread('fi ...
- R语言读取excel文件的3种方法
R读取excel文件中数据的方法: 电脑有一个excel文件,原始的文件路径是:E:\R workshop\mydata\biom excel数据为5乘2阶矩阵,元素为 ...
- Java读取Excel文件的几种方法
Java读取 Excel 文件的常用开源免费方法有以下几种: 1. JDBC-ODBC Excel Driver 2. jxl.jar 3. jcom.jar 4. poi.jar 简单介绍: 百度文 ...
- java 分次读取大文件的三种方法
1. java 读取大文件的困难 java 读取文件的一般操作是将文件数据全部读取到内存中,然后再对数据进行操作.例如 Path path = Paths.get("file path&qu ...
- 读取Excel文件的两种方法
第一种方法:传统方法,采用OleDB读取EXCEL文件, 优点:写法简单,缺点:服务器必须安有此组件才能用,不推荐使用 private DataSet GetConnect_DataSet2(stri ...
- .NET读取Excel文件的三种方法的区别
ASP.NET读取Excel文件方法一:采用OleDB读取Excel文件: 把Excel文件当做一个数据源来进行数据的读取操作,实例如下: public DataSet ExcelToDS(strin ...
- C#读取资源文件的两种方法及保存资源文件到本地
方法1 GetManifestResourceStream VB.NET中资源的名称为:项目默认命名空间.资源文件名 C#中则是:项目命名空间.资源文件所在文件夹名.资源文件名 例如:istr = ...
- PHP读取大文件的几种方法介绍
读取大文件一直是一个头痛的问题,我们像使用php开发读取小文件可以直接使用各种函数实现,但一到大文章就会发现常用的方法是无法正常使用或时间太长太卡了,下面我们就一起来看看关于php读取大文件问题解决办 ...
随机推荐
- LeetCode: Recover Binary Search Tree [099]
[题目] Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without cha ...
- 2018.10.10 MAC 的Launchpad图标改变大小的设置
mac更改launchpad图标大小 设置每列显示的图标数目为8 defaults write com.apple.dock springboard-columns -int 8 设置每行显示的图标数 ...
- 【luogu P3369 【模板】普通平衡树(Treap/SBT)】 模板 Scapegoat Tree
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> ...
- c语言描述的直接插入排序法
#include<stdio.h> #include<stdlib.h> #define SIZE 6 typedef int Type; //直接插入排序法 void Ins ...
- Knowledge Point 20180305 机器数转换与进制转换
机器数(这里的机器数说的就是数值在计算机中的存储形式,相关可以了解数据在计算机中的表示)之间的转换往往是通过原码来实现的,下面我们结合进制来来一下: 进制也就是进位制,是人们规定的一种进位方法. 对于 ...
- linux 怎么查看系统的环境变量 与设置jdk 系统环境变量
1.win 7 ,win10 怎么查看,添加系统环境的变量,大家都非常清楚的.但是linux 的 却不一定哦. 打开终端输入 : “echo $PATH “ or “export ” 如 ...
- oracle https://localhost:1158/em 无法打开
解决办法一: 首先查看本机Oracle安装路径中 portlist.ini 文件里面的端口号是多少,例如我的就是5500. 那么在浏览器中输入的地址就是:https://localhost:5500/ ...
- excel导入到java/导出到excel
package com.test.order.config; import com.test.order.domain.HavalDO; import org.apache.poi.ss.usermo ...
- VMware ESXi-6.7——使用
1: 上传ISO文件 1.1:创建一个新目录,上传ISO 1.2: 在新建虚拟机时,点击DVD,选择数据ISO文件,选择要安装的ISO文件.并把连接打钩. 2:新建虚拟机 按照需求填写 硬盘的三种置备 ...
- Eclipse关联tomcat
一,添加Tomcat Windows-->Preferences-->Server-->Runtime Enviroment添加一个tomcat,这里选择tomcat8.0 Next ...