classLoader读取文件与文件流读取文件示例与注意事项

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{
//      test0();
        test2();
    }
    /**
     * path前缀不带文件分隔符的情况
     * properties文件存放路径为
     * src+包名+文件名
     * @throws IOException
     */
    public static void test0() throws IOException{
        String file="email.properties";
        Properties prop=getProperties(file);
        prop.put("new", "new");
        //全路径src+包名+文件名
        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) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            if (resourceAsStream!=null) {
                try {
                    resourceAsStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    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) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            if (resourceAsStream!=null) {
                try {
                    resourceAsStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值