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

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:/” 的长度是6
			p.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 1
		loadBySystemResourceStream();
		
		// 方式2
		loadByCurentClassLoader();
		
		// 方式3 1
		loadByCurrentClass();
		
		// 方式4
		loadByInputSrtream();
		
		// 方式5
		loadByPropertyResourceBundle();
		
		// 方式6
		loadByResourceBundle();
	}

}

message.properties:

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值