---
title: "读取.properties文件"
author: "记录成长"
date: "2010-10-19T15:20:37Z"
tags: []
categories: ["企业架构"]
slug: "properties"
draft: false
lastmod: "2010-10-19T15:20:37Z"
id: 788582
url: "https://cjzuo-java-gmail-com.iteye.com/blog/788582"
---

1、chat.properties 一般都写成key,value的形式，注释用#。

 

```
#Generated by ManageChatServlet
#Mon Apr 23 11:15:57 GMT+05:30 2001
Jsp=Discussion about JSP can be made here.
Java=Talk about java and related technologies like J2EE.
ASP=Discuss about Active Server Pages related technologies like VBScript and JScript etc.
Web_Designing=Any discussion related to HTML, JavaScript, DHTML etc.
StartUp=Startup chat room. Chatter is added to this after he logs in.
```

 

 

2、这是一个servlet中的初始化方法，在web.xml中配置，只要tomcat容器起动，就执行这个初始化方法。

public void init() throws ServletException

```
	{       Properties props = null;
		try
		{
			String path = "";
```

```
			//加载web.xml中的参数,properties的文件名
			path = "/WEB-INF/"+getServletContext().getInitParameter("chatpropertyfile");
			String realPath;
			realPath = getServletContext().getRealPath(path);
			
			if (realPath != null)
			{
				InputStream fis = new FileInputStream(realPath);

				props = new Properties();
				props.load(fis);
				Enumeration keys = props.keys();
				while (keys.hasMoreElements())
				{
					String roomName = (String)keys.nextElement();
					String roomDescr = (String)props.getProperty(roomName);
					addNewRoom(rooms, roomName, roomDescr);
				}
				fis.close();
				getServletContext().setAttribute("chatroomlist", rooms);
				System.err.println("Room List Created");
			}
			else
			{
				System.out.println("Unable to get realpath of chatproperty file " + path + ".\nCheck that application war file is expanded and file can be read.\nChat application won't work.");
			}
		}
		catch(FileNotFoundException fnfe)
		{
			System.err.println("Properites file not found:" + fnfe.getMessage());
		}
		catch(IOException ioe)
		{
			System.out.print("Unable to load Properties File: " + ioe.getMessage());
		}		
	}
```

 

3、在web.xml中，参数可以这样写在xml，我要记一下的。。

 

```
<context-param>
		<param-name>chatpropertyfile</param-name>
		<!--  Name of the chat properties file. It contains the name and description of rooms.
		-->		
		<param-value>chat.properties</param-value>
	</context-param>

	<context-param>
		<param-name>saveRooms</param-name>
		<!-- The value of saveRooms can be set to true or false.
		If true then all new rooms added by users will be saved in chat.properties files.
		Default is false.
		-->		
		<param-value>false</param-value>
	</context-param>

	<context-param>
		<param-name>adminEmail</param-name>
		<!-- Add an e-mail address of the administrator of this web site. -->
		<param-value>admin@website.com</param-value>
	</context-param>
```

  下面这段中&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;是让tomcat一起动，就加载这个servlet。

 

当值为0或者大于0时，表示容器在应用启动时就加载这个servlet；

当是一个负数时或者没有指定时，则指示容器在该servlet被选择时才加载。

正数的值越小，启动该servlet的优先级越高。

 

```
         <servlet>
		<servlet-name>manageChat</servlet-name>
		<servlet-class>sukhwinder.chat.servlet.ManageChatServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
```

 

 

上传一个我在国外的网上下载的用jsp+servlet实现的聊天系统，自认为很有参考价值。。

chat.rar是不用IDE的版本

chat-myeclipse.rar是myeclipse的版本。