读取配置文件
来源:互联网 发布:php模板引擎好不好 编辑:程序博客网 时间:2024/06/08 16:58
类加载器
1、直接用Class.ForName(类的路径名)
Class.forName("com.hsf.ClassLoader.ClassLoader");
2、用ClassLoder (类加载器)
InputStream in = ClassLoader.getSystemResourceAsStream("aa.txt"); byte[] bs= new byte[1024]; try { int len=in.read(bs); //把字节转换成String System.out.println(new String(bs,0,len)); } catch (IOException e) { e.printStackTrace(); }```
小知识点:
1、把字节转换成String
System.out.println(new String(bs,0,len));
2、将String转换成字节数组
String string="aaaa";byte[] bytes = string.getBytes();
配置文件的读写
数据库的账号和密码可能会发生变更,所以这些都不应该写在程序中,而应该写在配置文件中,读取配置文件,这样修改起来也比较方便。
以下是读取配置文件的代码:
static { try { Properties properties = new Properties(); // JdbcUtil.class.getClassLoader(); InputStream in =JdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"); System.out.println("in="+in); //InputStream in = ClassLoader.getResourceAsStream("jdbc.properties"); //装载文件 properties.load(in); forName = properties.getProperty("forName"); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); //不需要每次都加载一次驱动 Class.forName(forName); } catch (Exception e) { e.printStackTrace(); } }
注意:配置文件里面字符串一定不要用“”,在写的过程中由于password为空,在配置文件里面写成了password =“”,所以就报了数据库连接错误。还有就是 InputStream in =JdbcUtil.class.getClassLoader().getResourceAsStream(“jdbc.properties”); 这里一定不能写成getSystemResourceAsStream。
阅读全文
0 0
- 读取配置文件
- 读取 配置文件
- 读取配置文件
- 读取配置文件
- 配置文件读取
- 配置文件读取
- 读取配置文件
- 读取配置文件
- 读取配置文件
- 读取配置文件
- 读取配置文件
- 读取配置文件
- 读取配置文件
- 读取配置文件
- 读取配置文件
- 读取配置文件
- 配置文件读取
- 读取配置文件
- PTA—11-散列4 Hashing
- 应用进程重复启动防范和控制方法
- 主题模型LDA
- Python eval 函数妙用
- Spring对象JDBC的支持
- 读取配置文件
- https协议
- AlexNet论文《ImageNet Classification with Deep Convolutional Neural Networks》总结
- JavaScript 跨frame或iframe操作对象 | 黄乔国PHP
- angular directive详解之replace
- Day02 varnish cdn推送
- Python实现MySQL DBA小工具一例
- linux --输入输出的管理
- 万众瞩目Instant Apps终于全面问世啦