Java读取属性Properties文件的方法
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; public class PropertyReader { private Properties prop; private String path; public PropertyReader(String path) { this.prop=new Properties(); this.path=path; try { FileInputStream in=new FileInputStream(new File(this.path)); this.prop.load(in); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public String getProperty(String key) { return prop.getProperty(key); } public void addProperty(String key,String value) { prop.put(key, value); } }
?
应用
String path="conf.properties";PropertyReader propertyReader=new PropertyReader(path);byestr=propertyReader.getProperty("byeStr");addr=propertyReader.getProperty("address");sendPort=Integer.parseInt(propertyReader.getProperty("sendPort") );dstPort=Integer.parseInt(propertyReader.getProperty("dstPort"));?
?properties文件路径从classes目录下开始,即和class文件放置在一起
?
properties文件
#接收方地址、端口链接信息address=192.168.1.100dstPort=10000receivePort=10000#发送方链接信息sendPort=8082#结束发送标识byeStr=886
?
?