---
title: "读取配置文件的方法"
author: "Ivan0513"
date: "2013-04-22T21:27:07Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2013-04-22T21:27:07Z"
id: 1851649
url: "https://ivan0513.iteye.com/blog/1851649"
---

使用Eclipse开发，将properties文件放在src文件夹下（生成class时，properties文件也会拷贝到bin下）

使用ClassLoader.getSystemResourceAsStream方法得到InputStream，

再用Properties的load方法加载属性文件

 

```
package ex.properties;

import java.io.IOException;
import java.util.Properties;

public class LoadProperties {

	public static void main(String[] args) throws IOException {
		//String myPath = System.getProperty("user.dir");   
		
		Properties p = new Properties();

		p.load(ClassLoader.getSystemResourceAsStream("Hello.properties"));
		
		System.out.println(p.getProperty("word"));
	}
}
```

 

这样就不会出现找不到文件的情况出现了。