我爱学Java之读取Properties的几种方法
来源:互联网 发布:office2011 for mac 编辑:程序博客网 时间:2024/06/05 10:59
首先贴出目录结构:
db.properties
1.Properties类的load()方法:
public static void main(String[] args) throws IOException{ InputStream in = new BufferedInputStream(new FileInputStream("src/db.properties"));//相对于根路径 Properties p = new Properties(); p.load(in); System.out.println(p.getProperty("user")); }
2.使用class变量的getResourceAsStream()方法
public static void main(String[] args) throws IOException{ InputStream in = Properties.class.getResourceAsStream("/db.properties");//不以"/"开头则默认为此类所在包下的资源,以’/'开头则是从ClassPath根下获取,ClassPath可以通过System.getProperty("java.class.path")获取。 Properties p = new Properties(); p.load(in); System.out.println(p.getProperty("user")); }
3.使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
public static void main(String[] args) throws IOException{ InputStream in = ClassLoader.getSystemResourceAsStream("db.properties");//从classpath路径下 Properties p = new Properties(); p.load(in); System.out.println(p.getProperty("user")); }
4.使用class.getClassLoader().getResourceAsStream()方法
public static void main(String[] args) throws IOException{ InputStream in = Test.class.getClassLoader().getResourceAsStream("db.properties");//从classpath路径下,Test为运行该代码的类 Properties p = new Properties(); p.load(in); System.out.println(p.getProperty("user")); }
5.使用java.util.ResourceBundle类的getBundle()方法
public static void main(String[] args) throws IOException{ ResourceBundle rb = ResourceBundle.getBundle("db"); System.out.println(rb.getString("user")); }
6.使用java.util.PropertyResourceBundle类的构造函数
public static void main(String[] args) throws IOException{ InputStream in = new BufferedInputStream(new FileInputStream("src/db.properties")); ResourceBundle rb = new PropertyResourceBundle(in); System.out.println(rb.getString("user")); }
运行结果都为:
0 0
- 我爱学Java之读取Properties的几种方法
- java 读取Properties 几种方法
- Java读取Properties文件的几种方法
- Java 读取Properties文件的几种方法
- java读取.properties配置文件的几种方法
- java读取.properties配置文件的几种方法
- java几种读取.properties文件的方法
- java读取.properties配置文件的几种方法 .
- java读取properties文件的几种方法
- java读取properties文件的几种方法
- java读取.properties配置文件的几种方法
- java读取properties文件的几种方法
- java读取properties文件的几种方法
- java读取properties文件的几种方法
- java读取properties文件的几种方法
- java读取.properties配置文件的几种方法
- java读取properties文件的几种方法
- java读取properties文件的几种方法
- 有关于mfc webbrowser插件的使用
- Android 实现耳机hook键长按
- matlab 画图中线型及颜色设置
- web 获取select中被选中option的value和text
- Android之多线程 AsyncTask 详解
- 我爱学Java之读取Properties的几种方法
- MINA学习
- C笔记
- linux中fork()函数详解(原创!!实例讲解) (转载)
- leetcode125.ValidPalindrome
- FMDB性能优化问题。使用FMDB事务批量更新数据库速度问题。(亲测可以呀---740条数据用和不用事务效率差别20倍+)
- TCP协议中的三次握手和四次挥手(图解)
- POST & GET & Ajax 全解
- 使用IIS实现域名跳转