Java读取properties文件的三种方式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java读取properties文件的三种方式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3900字,纯文字阅读大概需要6分钟。
内容图文
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.Properties; import org.springframework.core.io.support.PropertiesLoaderUtils; public class PropertiesTest { /** * 根据key读取value(第一种方式 ) * * @param filePath * 相对路径, properties文件需在classpath目录下, * 比如:configure.properties在包com.test.configure下, * 路径就是com/test/configure/configure.properties * @param key * @return */ public static String getProperty(String filePath, String key) { String value = null; java.util.Properties props; try { props = PropertiesLoaderUtils.loadAllProperties(filePath); value = props.getProperty(key); } catch (IOException e) { e.printStackTrace(); } return value; } /** * 读取properties的全部信息(第一种方式 ) * * @param filePath * 相对路径, properties文件需在classpath目录下, * 比如:configure.properties在包com.test.configure下, * 路径就是com/test/configure/configure.properties */ public static void getProperties(String filePath) { java.util.Properties props; try { props = PropertiesLoaderUtils.loadAllProperties(filePath); printAllProperty(props); } catch (IOException e) { e.printStackTrace(); } } /** * 根据key读取value(第二种方式 ) * * @param filePath * 绝对路径或相对路径, 如果是相对路径,则从当前项目下的目录开始计算, * 如:当前项目/configure/configure.properties, * 相对路径就是configure/configure.properties * @param key * @return */ public static String readValue(String filePath, String key) { Properties props = new Properties(); String value = null; try { InputStream in = new BufferedInputStream(new FileInputStream(filePath)); props.load(in); value = props.getProperty(key); } catch (Exception e) { e.printStackTrace(); } return value; } /** * 读取properties的全部信息(第二种方式 ) * * @param filePath * 绝对路径或相对路径, 如果是相对路径,则从当前项目下的目录开始计算, * 如:当前项目/configure/configure.properties, * 相对路径就是configure/configure.properties */ public static void readProperties(String filePath) { Properties props = new Properties(); try { InputStream in = new BufferedInputStream(new FileInputStream(filePath)); props.load(in); printAllProperty(props); } catch (Exception e) { e.printStackTrace(); } } /** * 根据key读取value(第三种方式 ) * * @param filePath * 相对路径, properties文件需在classpath目录下, * 比如:configure.properties在包com.test.configure下, * 路径就是/com/test/configure/configure.properties * @param key * @return */ public static String getValue(String filePath, String key) { Properties props = new Properties(); try { // InputStream in = Object.class.getResourceAsStream(filePath); props.load(in); } catch (IOException e) { e.printStackTrace(); } return props.getProperty(key); } /** * 读取properties的全部信息(第三种方式 ) * * @param filePath * 相对路径, properties文件需在classpath目录下, * 比如:configure.properties在包com.test.configure下, * 路径就是/com/test/configure/configure.properties * @return */ public static void getValues(String filePath) { Properties props = new Properties(); try { InputStream in = Object.class.getResourceAsStream(filePath); props.load(in); printAllProperty(props); } catch (IOException e) { e.printStackTrace(); } } private static void printAllProperty(java.util.Properties props) { @SuppressWarnings("rawtypes") Enumeration en = props.propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); String value = props.getProperty(key); System.out.println(key + value); } } public static void main(String[] args) { System.out.println(getProperty("com/test/configure/configure.properties", "jdbc.driverClassName")); getProperties("com/test/configure/configure.properties"); System.out.println(readValue("configure/configure.properties", "jdbc.driverClassName")); readProperties("configure/configure.properties"); System.out.println(getValue("/com/test/configure/configure.properties", "jdbc.driverClassName")); getValues("/com/test/configure/configure.properties"); } }
? ?转载请注明出处:http://xieke90.iteye.com/blog/2233046
原文:http://xieke90.iteye.com/blog/2233046
内容总结
以上是互联网集市为您收集整理的Java读取properties文件的三种方式全部内容,希望文章能够帮你解决Java读取properties文件的三种方式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。