• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Java Property类使用详解

java 搞代码 4年前 (2022-01-05) 41次浏览 已收录 0个评论

这篇文章主要介绍了Java Property类使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

概念理解

Properties 继承于 Hashtable。表示一个持久的属性集,属性列表以key-value的形式存在,key和value都是字符串。Properties类被许多Java类使用。例如,在获取环境遍历时它就作为System.getProperties()方法的返回值。我们在很多需要避免硬编码的应用场景下需要使用Properties文件来加载程序需要配置的信息,比如JDBC、MyBatis框架等。Properties类则是Properties文件和程序的中间桥梁,不论是从properties文件读取信息还是写入信息到properties文件都要经由Properties类。

写入

Properties类调用setProperty方法将键值对保存到内存中,此时可以通过getProperty方法读取,propertyNames方法进行遍历,但是并没有将键值对持久化到属性文件中,故需要调用store方法持久化键值对到属性文件中。

我们写一个类测试

 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文件配置文件的各项属性。

 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

遍历

遍历属性文件中的键值对

 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 keys = prop.keySet(); for(Object key:keys) { System.out.println("key:" + key.toString() + "|" + "value:" + prop.get(key)); } //方法二: Set<Entry> entrys =	prop.entrySet();//返回的属性键值对实体 for(Entry 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,这样才会让配置文件保存中文数据的时候不会出现转码的问题

以上所述是小编给大家

来源gaodai.ma#com搞#代!码网

介绍的Java Property类使用详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对gaodaima搞代码网网站的支持!

以上就是Java Property类使用详解的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java Property类使用详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址