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
相关推荐
java 读取properties文件代码
java读写properties配置文件
Java读取Properties文件的六种方法
JAVA读取properties的值,JAVA读取properties文件,JAVA读取properties的内容
java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题
java如何读取properties配置文件
java实现properties文件读取
java读取properties文件的工具类,传入配置文件名字和其中的key就可以读取
利用Java的Properties 类读取配置文件信息! 读取Properties文件的例子
java读取properties文件六种方法
java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题
java完美读取properties文件
解决JAVA读取properties中文乱码问题
java读取properties文件,没有用到了第三方jar包,方便实用
读取properties文件返回map
ConfigFile configfile = ConfigFile.getInstance("ipConfig123.properties"); String ip = configfile.getkeyvalue("ip"); 可以取出ipConfig123.properties 文件中IP的内容