---
title: "Java - 读取Properties文件信息"
author: "topsale"
date: "2011-04-11T17:18:06Z"
tags: []
categories: ["编程语言"]
slug: "java-properties"
draft: false
lastmod: "2011-04-12T10:01:48Z"
id: 998079
url: "https://topsale.iteye.com/blog/998079"
---

读取Properties文件有6种方法，我就写一种我常用的，写多了没意思，能用就行呗。

Properties文件代码如下：

```java

hello_world = HelloWorld

```


Java文件代码如下：

```java

	public static void main(String[] args) {
		JSTLServlet test = new JSTLServlet();
		test.showProperties();
	}
	
	private void showProperties() {
		//获取根目录
		String filePath = this.getClass().getResource("/").getPath();
		try {
			//将Properties文件放入流中
			InputStream stream = new BufferedInputStream(new FileInputStream(new File(filePath + "test.properties")));
			Properties pro = new Properties();
			//加载
			pro.load(stream);
			//获取hello_world的值
			String str = pro.getProperty("hello_world");
			//输出
			System.out.println(str);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

```


输出结果：
HelloWorld