java读取src下面properties文件
来源:互联网 发布:襄县农村淘宝招聘 编辑:程序博客网 时间:2024/06/01 09:56
1.使用java.util.Properties类的load()方法
示例:
//文件在项目下。不是在包下!!
InputStream in = new BufferedInputStream(newFileInputStream("demo.properties")) ;
Properties p = new Properties();
p.load(in) ;
String className2 =p.getProperty("database.driver");
String url = p.getProperty("database.url");
String user =p.getProperty("database.user");
String password =p.getProperty("database.pass");
2. 使用java.util.Resourcebundle类的getbundle()方法
//前面没有“/”代表当前类的目录
示例:
//文件和类在同一个包下,注意它的文件名和后缀!!是调换的,
ResourceBundle resource =ResourceBundle.getBundle("properties.jdbc");
String className =resource.getString("database.driver");
String url =resource.getString("database.url");
String user =resource.getString("database.user");
String password =resource.getString("database.pass");
3.使用java.util.PropertyResourceBundle类的构造函数
示例:
// 文件在项目下 或者 src/demo.properties
// 在 src/demo.properties 写成 new FileInputStream("src/demo.properties")
InputStream in = new BufferedInputStream(new FileInputStream("demo.properties"));
ResourceBundle rb = newPropertyResourceBundle(in) ;
String className4 =rb.getString("database.url");
4.使用class变量的getresourceasstream()方法
示例:
InputStream in=Properties.class.getResourceAsStream("/properties/jdbc.properties");
// 包点类名下的。
// 如果找不到带有该名称的资源,则返回 null
Properties p = new Properties();
p.load(in);
System.out.println(p.getProperty("database.url"));
5.使用class.getclassloader()所得到的java.lang.classloader的getresourceasstream()方法
// properties 文件 要放在src下面,否则找不到啊
示例:
InputStream in = 类名.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties p = new Properties() ;
p.load(in);
System.out.println(p.getProperty("database.pass"));
6.使用java.lang.classloader类的getsystemresourceasstream()静态方法
示例:
// 同包名下
InputStream in =ClassLoader.getSystemResourceAsStream("properties/jdbc.properties");
Properties p = new Properties() ;
p.load(in) ;
System.out.println(p.getProperty("database.user"));
总结:如果是 在WEB上读取properties文件,写成下面这种。上面写的那些只在 JavaSE 中
String path =Thread.currentThread().getContextClassLoader().getResource("").getPath();
System.out.println(path);
InputStreamin = new FileInputStream(newFile(path+File.separator+"mysql.properties"));
Properties prop = new Properties();
- java读取src下面properties文件
- java读取src下面的目录
- 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文件
- HIbernate的概述
- C++中类成员函数作为回调函数
- mac osx 中 runModalWindow 阻塞usb数据接收线程
- csdn搜索自己的文章
- 234. Palindrome Linked List
- java读取src下面properties文件
- Linux出现问题1
- Access violation at address 77106D4E in module 'ntdll.dll'. Write of address 004051A5.
- Android 禁止 ListView 上下滑动
- csu1224 ACM小组的古怪象棋
- css控制单行文本溢出
- 2017
- jq属性选择器(多重属性选择)
- Android 是否禁止 viewpager 左右滑动