概念理解
properties 继承于 hashtable。表示一个持久的属性集,属性列表以key-value的形式存在,key和value都是字符串。properties类被许多java类使用。例如,在获取环境遍历时它就作为system.getproperties()方法的返回值。我们在很多需要避免硬编码的应用场景下需要使用properties文件来加载程序需要配置的信息,比如jdbc、mybatis框架等。properties类则是properties文件和程序的中间桥梁,不论是从properties文件读取信息还是写入信息到properties文件都要经由properties类。
写入
properties类调用setproperty方法将键值对保存到内存中,此时可以通过getproperty方法读取,propertynames方法进行遍历,但是并没有将键值对持久化到属性文件中,故需要调用store方法持久化键值对到属性文件中。
我们写一个类测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.outputstream; import java.util.date; import java.util.properties;
public class testproperties { public void writeproperties() { properties properties = new properties(); outputstream output = null ; try { output = new fileoutputstream( "config.properties" ); properties.setproperty( "url" , "jdbc:mysql://localhost:3306/" ); properties.setproperty( "username" , "root" ); properties.setproperty( "password" , "root" ); properties.setproperty( "databases" , "music_player" ); properties.store(output, "steven1997 modify" + new date().tostring()); } catch (ioexception e) { e.printstacktrace(); } finally { if (output!= null ) { try { output.close(); } catch (ioexception e) { e.printstacktrace(); } } }
} public static void main(string[] args) { testproperties t = new testproperties(); t.writeproperties(); } } |
执行后,工程下面会出现一个config.properties文件,属性文件内容如下:
读取
使用getproperty获取config.properties文件配置文件的各项属性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package property;
import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.util.properties;
public class loadproperties { public void loadproperties() { properties properties = new properties(); inputstream inputstream = null ;
try { inputstream = new fileinputstream( "config.properties" ); properties.load(inputstream); system.out.println( "url:" + properties.getproperty( "url" )); system.out.println( "username:" + properties.getproperty( "username" )); system.out.println( "password:" + properties.getproperty( "password" )); system.out.println( "database:" + properties.getproperty( "database" )); } catch (ioexception e) { e.printstacktrace(); } finally { if (inputstream != null ) { try { inputstream.close(); } catch (ioexception e) { e.printstacktrace(); } } }
} public static void main(string[] args) { loadproperties l = new loadproperties(); l.loadproperties(); } } |
运行后的结果
url:jdbc:mysql://localhost:3306/
username:root
password:root
database:music_player
遍历
遍历属性文件中的键值对
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package property;
import java.io.inputstream; import java.util.enumeration; import java.util.map.entry; import java.util.properties; import java.util.set;
public class propertiestest { public void printall() { properties prop = new properties(); inputstream input = null ; try { string file = "config.properties" ; input = getclass().getclassloader().getresourceasstream(file); if (input == null ) { system.out.println( "无法加载文件" + file); return ; } prop.load(input); // 方法一 set<object> keys = prop.keyset(); for (object key:keys) { system.out.println( "key:" + key.tostring() + "|" + "value:" + prop.get(key)); } //方法二: set<entry<object, object>> entrys = prop.entryset(); //返回的属性键值对实体 for (entry<object, object> entry:entrys){ system.out.println( "key:" +entry.getkey()+ ",value:" +entry.getvalue()); } //方法三: enumeration<?> e = prop.propertynames(); while (e.hasmoreelements()) { string key = (string) e.nextelement(); string value = prop.getproperty(key); system.out.println( "key:" + key + ",value:" + value); }
} catch (exception e) { e.printstacktrace(); } finally { if (input != null ) { try { input.close(); } catch (exception e) { e.printstacktrace(); } } } } public static void main(string[] args) { propertiestest p = new propertiestest(); p.printall(); } } |
运行结果如下:
key:url|value:jdbc:mysql://localhost:3306/
key:password|value:root
key:database|value:music_player
key:username|value:root
key:url,value:jdbc:mysql://localhost:3306/
key:password,value:root
key:database,value:music_player
key:username,value:root
key:password,value:root
key:url,value:jdbc:mysql://localhost:3306/
key:database,value:music_player
key:username,value:root
properties文件默认的编码格式居然是iso-8859-1,这样导致往配置文件里面写入中文的时候转换成另一种格式的编码,需要把properties 文件的编码格式改为utf-8,这样才会让配置文件保存中文数据的时候不会出现转码的问题
以上所述是小编给大家介绍的java property类使用详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
原文链接:https://blog.csdn.net/bo123_/article/details/89059799
查看更多关于Java Property类使用详解的详细内容...