java中动态的读取src下面的配置文件,动态的读取同个包下面的配置文件。

来源:互联网 发布:linux gtk编程 编辑:程序博客网 时间:2024/05/19 21:59

有时候我们要读取src文件下的文件。这时就要知道文件的路径。

一种方法是显示的给出文件的路径。

例如:PrintProperties.printProperties("D:\\dev\\workspase2Spring\\MyCommit\\src\\b.properties");

这样在本机运行运行起来是可以的。

另一种方法是动态的获取路径,读取src下的配置文件

这里我写了个工具类来获取src路径。

工程目录:


配置文件b.properties中的内容:

lan=123blue=234green=2345red=456

工具类:得到src目录下文件的路径

package com.lan.Properties;public class FilePathInSrcAfterRun{/*String path=MySrcFileLocationAfterCompile.class.getClassLoader().getResource("").toString();编译之后src目录下面的文件,会在bin目录中,bin下的文件或者文件夹与src下的文件或者文件夹一一对应System.out.println("src目录下的文件经过编译后的位置:"+path);*/public String getFilePathInSrcAfterRun(){String path = this.getClass().getClassLoader().getResource("").toString();// System.out.println("编译后src路径:"+path);//file:/D:/dev/workspase2Spring/XMLreader/bin/int m = path.indexOf("/");// file:/<----点位到file:后面的反斜杠path = path.substring(m + 1);// 从反斜杠之后的一位开始截取字符串// System.out.println("编译后src路径:"+path);return path;}}
工具类:打印配置文件

package com.lan.Properties;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Iterator;import java.util.Properties;import java.util.Set;public class PrintProperties{public static void printProperties(String filePath){Properties properties=new Properties();    //新建一个配置文件类型。这个类型是一个键值对列表,类似于Map ,    //Properties类实现了Map接口    //Properties相当于只能存放String类型的键值对的Map集合            try{    InputStream inStream=new BufferedInputStream(    new FileInputStream(filePath));properties.load(inStream);Set<String> keys=properties.stringPropertyNames();    //取出所有的键,作为键的Set集合    for(Iterator<String> it=keys.iterator();it.hasNext();)    {    String key=it.next();//取出下一个键key    String value=properties.getProperty(key);    //通过键从a.properties(相当于键值对Set集合)中取出value    System.out.println("key=\t"+key+"\tvalue="+value);    }} catch (IOException e){// TODO Auto-generated catch blocke.printStackTrace();}}}

测试:

package com.lan.Properties;public class FilePathInSrcAfterRunTest{    public static void main(String[] args)    {        String path=new FilePathInSrcAfterRun().getFilePathInSrcAfterRun();        System.out.println("编译之后的src路径:"+path);        String filePath=path+"b.properties";        System.out.println("编译之后src下的文件路径:"+filePath);        //打印配置文件        PrintProperties.printProperties(filePath);        System.out.println("---------------------------------------");        //使用显示路径:D:\dev\workspase2Spring\MyCommit\src\b.properties        PrintProperties.printProperties("D:\\dev\\workspase2Spring\\MyCommit\\src\\b.properties");    }}
运行结果:

编译之后的src路径:D:/dev/workspase2Spring/MyCommit/bin/编译之后src下的文件路径:D:/dev/workspase2Spring/MyCommit/bin/b.propertieskey=bluevalue=234key=lanvalue=123key=greenvalue=2345key=redvalue=456---------------------------------------key=bluevalue=234key=lanvalue=123key=greenvalue=2345key=redvalue=456
可以看到这两种方法都能读取src下面的b.properties配置文件。

但第二种方法就比较灵活,就算工程路径改变了,只要b.properties配置文件还在src目录下面,就可以读取它。

第一种方法,每次改变工程路径,都要再次改变一下文件的路径

这里再来介绍读取同个包下面的配置文件的方法

配置文件a.properties中的内容:(乱写的一些东西)

bule=lanlan=bulegreen=yesyes=green
工具类:

package com.lan.Properties;public class FilePathInPakageAfterRun{public String ThisPackagePath(){String thisPackagePath=this.getClass().getResource("").toString();//System.out.println("路径:"+thisPackagePath);int m=thisPackagePath.indexOf("/");//去掉前面的file:thisPackagePath=thisPackagePath.substring(m+1);//System.out.println("路径:"+thisPackagePath);return thisPackagePath;//返回当前包返回的路径。}}
测试:

package com.lan.Properties;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Iterator;import java.util.Properties;import java.util.Set;public class FilePathInPakageAfterRunTest{    public static void main(String[] args)    {        String thisPackagePath=new FilePathInPakageAfterRun().ThisPackagePath();        System.out.println("当前包编译后的路径为:"+thisPackagePath);        String filePath=thisPackagePath+"a.properties";        PrintProperties.printProperties(filePath);                //        Properties properties=new Properties();//        //新建一个配置文件类型。这个类型是一个键值对列表,类似于Map ,//        //Properties类实现了Map接口//        //Properties相当于只能存放String类型的键值对的Map集合//        String APath=thisPackagePath+"a.properties";//        System.out.println("文件路径为:"+APath);//        try//        {//            InputStream inStream=new BufferedInputStream(//                    new FileInputStream(APath));//            properties.load(inStream);//            Set<String> keys=properties.stringPropertyNames();//            //取出所有的键,作为键的Set集合//            for(Iterator<String> it=keys.iterator();it.hasNext();)//            {//                String key=it.next();//取出下一个键key//                String value=properties.getProperty(key);//                //通过键从a.properties(相当于键值对Set集合)中取出value//                System.out.println("key=\t"+key+"\tvalue="+value);//            }//        } catch (IOException e)//        {//            // TODO Auto-generated catch block//            e.printStackTrace();//        }
当前包编译后的路径为:D:/dev/workspase2Spring/MyCommit/bin/com/lan/Properties/key=bulevalue=lankey=lanvalue=bulekey=greenvalue=yeskey=yesvalue=green


//        
    }
    
}

结果:

当前包编译后的路径为:D:/dev/workspase2Spring/MyCommit/bin/com/lan/Properties/key=bulevalue=lankey=lanvalue=bulekey=greenvalue=yeskey=yesvalue=green




原创粉丝点击