读取properties文件

properties文件一般用来存放系统配置文件:

举个例子:netconfig.properties(#为注释部分,然后是键值对)

#服务器ip和端口  
server=192.168.0.119
port=53303
MSG=这是一个很长的字符串,<br>我们可以使其换行 \一行显示不下时,<br>可以分行显示。

解析读取properties文件:

GetPropertiesUtil.java
 1 package com.sohan.util;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Properties;
6
7 public class GetPropertiesUtil {
8 /**
9 * 得到netconfig.properties配置文件中的所有配置属性
10 *
11 * @return Properties对象
12 */
13 public static Properties getNetConfigProperties() {
14 Properties props = new Properties();
15 InputStream in = GetPropertiesUtilcopy.class.getResourceAsStream("/netconfig.properties");
16 try {
17 props.load(in);
18 } catch (IOException e) {
19 e.printStackTrace();
20 }
21 return props;
22 }
23 }

  读取properties的方法:

	static String servername = GetPropertiesUtilcopy.getNetConfigProperties().getProperty("server");  
	static int serverport = Integer.parseInt(GetPropertiesUtilcopy.getNetConfigProperties().getProperty("port")); 
原文地址:https://www.cnblogs.com/qsl568/p/2415502.html