---
title: "java中读取Properties文件"
author: "klcwt"
date: "2009-10-23T12:58:04Z"
tags: []
slug: "java-properties"
draft: false
lastmod: "2009-10-23T12:58:04Z"
id: 502542
url: "https://klcwt.iteye.com/blog/502542"
---

### [java 中读取Properties 文件](http://icemoon.iteye.com/blog/97874)

**关键字: java properties** 在java 中，可以用

java 代码

1. Properties  p =  new  Properties ();
2. InputStream is =  this .getClass().getResourceAsStream(propertiesFileName);
3. p.load(is);
4. is.close;

来读取。

如果properties 文件是在classpath的根目录下，如test.properties 在src目录下，则可以这样读取：

1. InputStream is =  this .getClass().getResourceAsStream(" test.properties " );

如果不在要目录下，则要加上相对于classpath要目录的路径。如在src目录下，有一个包com.myapp.resources,则可以如下读取：

1. InputStream is =  this .getClass().getResourceAsStream( "/com/myapp/resource/test.properties " );
2. 在读取后最后调用一次trim()，以免多了个空格就导致取不到相应的property