1、读取properties文件内容
InputStream in = NumTest.class.getResourceAsStream("/db.properties" );
Properties properties = new Properties();
properties.load(in);
String value = properties.getProperty("jdbc.url" );
System.out.println(value);
String url=ResourceBundle.getBundle("db" ).getString("jdbc.url" );
System.out.println(url);
2、读取classpath下文件
String fileName = NumTest.class.getResource("/db.properties" ).getFile();
System.out.println(fileName);-->/D:/WorkSpace/Eclipse/SSM/target/classes/db.properties
3、Web工程下读取文件方法,参考 链接地址
String fileName = getServletContext().getRealPath("/index.jsp" );
String fileName2 = getServletContext().getRealPath("/WEB-INF/a.properties" );
String fileName3 = ReadFile.class.getResource("/info.txt" ).getFile();
String fileName4 = ReadFile.class.getResource("/cn/itcast/config/info.txt" ).getFile();
使用ResourceBundle 快速读取src下properties文件
String value = ResourceBundle.getBundle("myproperties" ).getString("name" );
使用Properties类加载Properties文件
InputStream in = ReadFile.class.getResourceAsStream("/b.properties" );
Properties properties = new Properties();
properties.load(in);
String value2 = properties.getProperty("name" );