0、data.properties文件位置及内容
文件名为data.properties 本测试文件在java工程在src下(绝对路径的直接写全路径即可)。
//使用方法一ResourceBundle读取时出错,找不到文件,原因如下:
//原因一:文件名不对(注意:不加后缀 .properties。我就是这个问题,手残,找了很久)
//原因二:配置文件路径不对,我写的是java工程,需要把配置文件src下,如果放在包下需增加路径(如:放在src/com包下是“com.data”,本次测试在项目的src下,直接写"data"即可)
内容:
test1=test1Value
test2=test2Value
test3=test3Value
test4=test4Value
1、使用ResourceBundle
直接上代码:
package com.test;
import java.util.ResourceBundle;
public class GlobalTab {
private static GlobalTab gtb;
private GlobalTab() {
}
/**
* 获取实体
*
* @return
*/
public static GlobalTab getInstance() {
if (gtb == null) {
gtb = new GlobalTab();
}
return gtb;
}
public static String test1;
public static String test2;
public static String test3;
public static String test4;
static {
ResourceBundle rb = ResourceBundle.getBundle("data");
test1 = rb.getString("test1");
test2 = rb.getString("test2");
test3 = rb.getString("test3");
test4 = rb.getString("test4");
}
/**
* 测试
*
* @param args
*/
public static void main(String[] args) {
GlobalTab.getInstance();
System.out.println(GlobalTab.test1);
System.out.println(GlobalTab.test2);
System.out.println(GlobalTab.test3);
System.out.println(GlobalTab.test4);
}
}
测试结果
test1Value
test2Value
test3Value
test4Value
2、使用Properties,文件为绝对路径
package com.test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
public class GlobalTab2 {
private static GlobalTab2 gtb;
private GlobalTab2() {
}
/**
* 获取实体
*
* @param propertiesPath 配置文件全路径
* @return
*/
public static GlobalTab2 getInstance(String propertiesPath) {
if (gtb == null) {
gtb = new GlobalTab2(propertiesPath);
}
return gtb;
}
public static String test1;
public static String test2;
public static String test3;
public static String test4;
private GlobalTab2(String propertiesPath) {
try {
Properties properties = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(propertiesPath));
// properties.load(in);// 直接这么写,如果properties文件中有汉子,则汉字会乱码。因为未设置编码格式。
properties.load(new InputStreamReader(in, "utf-8"));
test1 = properties.getProperty("test1");
test2 = properties.getProperty("test2");
test3 = properties.getProperty("test3");
test4 = properties.getProperty("test4");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 测试
*
* @param args
*/
public static void main(String[] args) {
GlobalTab2.getInstance("E:\\propertiesTest\\src\\data.properties");
System.out.println(GlobalTab2.test1);
System.out.println(GlobalTab2.test2);
System.out.println(GlobalTab2.test3);
System.out.println(GlobalTab2.test4);
}
}
测试结果:
test1Value
test2Value
test3Value
test4Value
3、使用ResourceBundle读取文件返回所有
package com.test;
import java.util.ResourceBundle;
import java.util.Set;
public class GlobalTab3 {
public static ResourceBundle getResourceBundle() {
return ResourceBundle.getBundle("data");
}
/**
* 测试
*
* @param args
*/
public static void main(String[] args) {
ResourceBundle resourceBundle = GlobalTab3.getResourceBundle();
Set<String> keySet = resourceBundle.keySet();
for (String key : keySet) {
System.out.println(key + ":" + resourceBundle.getString(key));
}
}
}
测试结果:
test1:test1Value
test4:test4Value
test2:test2Value
test3:test3Value
4、使用Properties读取文件返回所有
package com.test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.Set;
public class GlobalTab4 {
public static Properties getProperties(String propertiesPath) {
Properties properties = null;
try {
properties = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(propertiesPath));
// properties.load(in);// 直接这么写,如果properties文件中有汉子,则汉字会乱码。因为未设置编码格式。
properties.load(new InputStreamReader(in, "utf-8"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
/**
* 测试
*
* @param args
*/
public static void main(String[] args) {
Properties properties = GlobalTab4.getProperties("E:\\skyechospace\\propertiesTest\\src\\data.properties");
Set<Object> keySet = properties.keySet();
for (Object object : keySet) {
System.out.println(properties.getProperty((String) object));
}
}
}
测试结果:
test4Value
test3Value
test2Value
test1Value
以上。