如何找到当前文件路劲和如何读取properties文件

来源:互联网 发布:linux 内核启动顺序 编辑:程序博客网 时间:2024/04/30 01:23

1、如何找到当前java类的路径 (LoadProperties是自定义的类)

①、LoadProperties.class.getResource(""):返回当前类LoadProperties.class文件的URI目录。不包括自己!

②、 得到的是当前的classpath的绝对URI路径。(以下四种方式皆是)

LoadProperties.class.getResource("/")

Thread.currentThread().getContextClassLoader().getResource("");

LoadProperties.class.getClassLoader().getResource("")

ClassLoader.getSystemResource("")

③、得到当前文件的系统路径 (以下两种皆是)

System.getProperty("user.dir")

new File("").getAbsolutePath()

/** * 显示当前文件的路劲 */public static void printCurrentDir(){// 得到的是当前类FileTest.class文件的URI目录。不包括自己!System.out.println(LoadProperties.class.getResource(""));// 得到的是当前的classpath的绝对URI路径。(以下四种方式皆是)System.out.println(LoadProperties.class.getResource("/"));System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));System.out.println(LoadProperties.class.getClassLoader().getResource(""));System.out.println(ClassLoader.getSystemResource(""));// 得到当前文件的路径System.out.println(System.getProperty("user.dir"));System.out.println(new File("").getAbsolutePath());}



2、读取properties文件

properties文件往往是用于存储一些适合保存在本地的信息,这些信息不适合用作静态变量,原因如果修改

这些变量,都会重新编译执行,浪费时间。很多应用都用到了这种文件来保存本地信息,如struts.properties,

在国际化处理中,也常用到xx_zh.properties..等。

如何读取呢?

①、使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法,即通过系统类加载器加载到inputstream:

InputStream is = ClassLoader.getSystemResourceAsStream(path);

②、使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法,即通过当前类的类加载器加载到inputstream

InputStream is = LoadProperties.class.getClassLoader().getResourceAsStream(path);

③、使用class变量的getResourceAsStream()方法,

InputStream is =  LoadProperties.class.getResourceAsStream(path);

④、通过InputStream (文件流)方式直接读入

InputStream is = new BufferedInputStream(new FileInputStream(path));

⑤、使用java.util.PropertyResourceBundle类的构造函数,该方式还是通过inputstream流读入的

InputStream is = new BufferedInputStream(new FileInputStream(path));

ResourceBundle rb = new PropertyResourceBundle(is);

⑥、使用java.util.ResourceBundle类的getBundle()方法

ResourceBundle rb = ResourceBundle.getBundle("Test201308/message",Locale.getDefault());// 第一个参数为包名+properties文件的名字(不要加后缀)


代码:

package Test201308;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.text.MessageFormat;import java.util.Iterator;import java.util.Locale;import java.util.Properties;import java.util.PropertyResourceBundle;import java.util.ResourceBundle;public class LoadProperties {// message.properties 和 LoadProperties.class在同级目录private static final String absolute_path = "Test201308/message.properties";private static final String relative_path = "message.properties";private static Properties p = new Properties();private static Object key;private static Object value;/** * 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法 */public static void loadBySystemResourceStream(){String path = absolute_path;InputStream is = ClassLoader.getSystemResourceAsStream(path);try {p.load(is);print();} catch (IOException e) {e.printStackTrace();}}/** * 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法 */public static void loadByCurentClassLoader(){InputStream is = LoadProperties.class.getClassLoader().getResourceAsStream(absolute_path);//message.properties在LoadProperties类同级目录下try {p.load(is);print();} catch (IOException e) {e.printStackTrace();}}/** * 使用class变量的getResourceAsStream()方法 */public static void loadByCurrentClass(){InputStream is =  LoadProperties.class.getResourceAsStream(relative_path);try {p.load(is);print();} catch (IOException e) {e.printStackTrace();}}/** * 通过InputStream 方式读入 */public static void loadByInputSrtream(){try {String path = LoadProperties.class.getResource("")+relative_path;InputStream is = new BufferedInputStream(new FileInputStream(path.substring(6)));// “file:/” 的长度是6p.load(is);print();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/** * 使用java.util.PropertyResourceBundle类的构造函数 */@SuppressWarnings("rawtypes")public static void loadByPropertyResourceBundle(){String path = System.getProperty("user.dir")+"/bin/"+absolute_path;try {InputStream is = new BufferedInputStream(new FileInputStream(path));ResourceBundle rb = new PropertyResourceBundle(is);// print Iterator it = rb.keySet().iterator();for(;it.hasNext();){key = it.next();value =  rb.getObject(key.toString());value = MessageFormat.format(value.toString(), new Object[]{"sb","NCBC"});System.out.println("key:"+key+" value:"+value);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/** * 使用java.util.ResourceBundle类的getBundle()方法 */@SuppressWarnings("rawtypes")public static void loadByResourceBundle(){ResourceBundle rb = ResourceBundle.getBundle("Test201308/message",Locale.getDefault());// print Iterator it = rb.keySet().iterator();for(;it.hasNext();){key = it.next();value = rb.getString(key.toString());value = MessageFormat.format(value.toString(), new Object[]{"sb","NCBC"});System.out.println("key:"+key+" value:"+value);}}/** * 打印显示 */@SuppressWarnings("rawtypes")public static void print(){Iterator keys = p.keySet().iterator();// 遍历数据while(keys.hasNext()){key = (String)keys.next();value = p.getProperty(key.toString());value= MessageFormat.format(value.toString(), new Object[]{"sb","NCBC"});System.out.println("key:"+key+" value:"+value);}}/** * 显示当前文件的路劲 */public static void printCurrentDir(){// 得到的是当前类FileTest.class文件的URI目录。不包括自己!System.out.println(LoadProperties.class.getResource(""));// 得到的是当前的classpath的绝对URI路径。(以下四种方式皆是)System.out.println(LoadProperties.class.getResource("/"));System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));System.out.println(LoadProperties.class.getClassLoader().getResource(""));System.out.println(ClassLoader.getSystemResource(""));// 得到当前文件的路径System.out.println(System.getProperty("user.dir"));System.out.println(new File("").getAbsolutePath());}/** * main 入口 * @param args */public static void main(String[] args) {printCurrentDir();// 方式1 1loadBySystemResourceStream();// 方式2loadByCurentClassLoader();// 方式3 1loadByCurrentClass();// 方式4loadByInputSrtream();// 方式5loadByPropertyResourceBundle();// 方式6loadByResourceBundle();}}

message.properties:

message = {0} says that message: {1}.


原创粉丝点击