classLoader读取文件与文件流读取文件示例与注意事项
package com.thatluck.res;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
/**
* 使用classLoader读取资源文件时的路径和文件流读取资源的路径是不一样的
* 使用classLoader读取资源文件时,路径分两种:
* 当path 带/指的是src所在目录
* 当path不带/指的是当前类文件目录
* 文件流读取资源的路径是另一种形式,默认当前路径为当前工程根目录
* 举例说明A(资源文件与类不在同一个目录下):
* * 工程目录是D:/awk/resdemo/
* 资源路径:D:/awk/resdemo/src/res/res.properties
* classLoader读取的文件路径为path="/res/res.properties";
* 而使用文件流方式保存文件时,默认当前路径为当前工程根目录即:D:/awk/resdemo/
* 注意正反斜杠
* path要修正为"src/res/res.properties"
*
* * 举例说明B(资源文件与类在同一个目录下):
* * 工程目录是D:/awk/resdemo/
* 资源路径:D:/awk/resdemo/src/com/thatluck/res/email.properties
* classLoader读取的文件路径为path="email.properties";
* 而使用文件流方式保存文件时,默认当前路径为当前工程根目录即:D:/awk/resdemo/
* 注意正反斜杠
* path要修正为"src/com/thatluck/res/email.properties"
* @throws IOException
*/
public class ResTest {
public static void main(String[] args) throws IOException{
test2();
}
/**
* path前缀不带文件分隔符的情况
* properties文件存放路径为
* src+包名+文件名
* @throws IOException
*/
public static void test0() throws IOException{
String file="email.properties";
Properties prop=getProperties(file);
prop.put("new", "new");
String fullpath="src/"+getPackagePath()+file;
System.out.println(fullpath);
FileOutputStream outputFile = new FileOutputStream(fullpath);
prop.store(outputFile, "new");
}
/**
* path前缀带文件分隔符的情况
*/
public static void test1() throws IOException{
String path="/res/res.properties";
Properties prop=getProperties(path);
prop.put("res", "res");
String fullpath="src"+path;
FileOutputStream outputFile = new FileOutputStream(fullpath);
prop.store(outputFile, "new");
}
public static void test3() throws IOException{
Properties prop=new Properties();
prop.put("test3", "test3");
String path="/test6.properties";
String fullpath="src"+path;
FileOutputStream outputFile = new FileOutputStream(fullpath);
prop.store(outputFile, "new");
}
public static void test2() throws IOException{
String path="email.properties";
Properties prop=getProperties(path);
Iterator it=prop.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry=(Map.Entry)it.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(key +":"+value);
}
}
public static String getPackageName(){
String packageName = ResTest.class.getPackage().getName();
System.out.println(packageName);
return packageName;
}
public static String getPackagePath(){
String name="";
String baseName=getPackageName()+".";
int index = baseName.lastIndexOf('.');
if (index != -1) {
name = baseName.substring(0, index).replace('.', '/')
+"/"+name;
}
return name;
}
public static Properties getPropertiesXml(String path){
Properties properties=null;
InputStream resourceAsStream = null;
try {
properties= new Properties();
resourceAsStream = ResTest.class.getResourceAsStream(path);
properties.loadFromXML(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
} finally{
if (resourceAsStream!=null) {
try {
resourceAsStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return properties;
}
/**
*
* @param path 带/指的是src根路径,不带/指的是当前类文件路径
* @return
*/
public static Properties getProperties(String path){
Properties properties=null;
InputStream resourceAsStream = null;
try {
properties= new Properties();
resourceAsStream = ResTest.class.getResourceAsStream(path);
properties.load(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
} finally{
if (resourceAsStream!=null) {
try {
resourceAsStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return properties;
}
}
properties文件也可以保存为简单xml格式,xml格式如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="hello">hello</entry>
</properties>