`
pluto418
  • 浏览: 166453 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

读取资源properties文件的写法

    博客分类:
  • J2SE
阅读更多
读取资源文件的方法有很多种,这种打jar是最方便的不会再报找不到文件了

import java.io.InputStream;
import java.util.Properties;

public class ReadFile {
	
	public static void main(String[] args) throws Exception {
		//相对路径
//		String fileName = "../../file.txt";
		String fileName = "../../file.properties";
		ReadFile rf = new ReadFile();
		rf.readProp(fileName);
	}

	public void readText(String fileName) throws Exception {
		InputStream is = this.getClass().getResourceAsStream(fileName);
		byte[] b = new byte[1024];
		is.read(b);
		System.out.println(new String(b).trim());
	}
	
	public void readProp(String fileName) throws Exception {
		
		//读取文件
		InputStream is = this.getClass().getResourceAsStream(fileName);
		Properties prop = new Properties();
		prop.load(is);
		System.out.println("key: " + prop.getProperty("key"));
	}

}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics