classpath路径和properties
在Java程序中,一般情况下使用绝对路径还是相对路径都不太合适,因为Java程序的jar包所放的位置不确定,执行java程序时当前的路径也不确定,所以不合适。一般在Java程序中我们会把资源放到classpath中,然后使用classpath路径查找资源。
1.获取classpath中的资源(InputStream)
public class Demo1 {
static Properties properties;
static{
properties = new Properties();
try {
Class clazz = Demo1.class;
// 开头的'/'表示classpath的根目录,这个是表示从classpath的根目录中开始查找资源,如果开头没有'/',表示从当前这个class所在的包中开始查找
InputStream inputestream = clazz.getResourceAsStream("/db.properties");
properties.load( inputestream);
} catch (IOException e) {
}
}
@Test
public void DBUtil(){
System.out.println("username:"+properties.getProperty("username")+
" password:"+properties.getProperty("password"));
}
}
2.Properties配置文件
加载配置文件
public class DBUtil {
static Properties properties = new Properties();
static{
try {
Class clazz = DBUtil.class;
InputStreamReader fileReader =
new InputStreamReader(clazz.getResourceAsStream("/db.properties"));
properties.load(fileReader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getUserName(){
String userName =properties.getProperty("userName");
return userName;
}
public static String getPassword(){
return properties.getProperty("password");
}
public static void main(String[] args) {
System.out.println("用户名:"+ getUserName());
System.out.println("密码: "+ getPassword());
}
}
写配置文件
public static void testStoreProperties() throws Exception {
// 准备配置信息
Properties properties = new Properties();
properties.setProperty("name", "李四");
properties.setProperty("age", "20");
// 准备
OutputStream out = new FileOutputStream("d:/my.properties");
String comments = "这是我的配置文件";
// 写出去
properties.store(out, comments);
out.close();
}
classpath路径和properties的更多相关文章
- web classpath 路径说明
classpath路径在每个J2ee项目中都会用到,即WEB-INF下面的classes目录,所有src目录下面的java.xml.properties等文件编译后都会在此,所以在开发时常将相应的xm ...
- classpath 及读取 properties 文件
java代码中获取项目的静态文件,如获取 properties 文件内容是必不可少的. Spring 下只需要通过 @Value 获取配置文件值 <!-- 资源文件--> <util ...
- Java项目中classpath路径
1.src不是classpath, WEB-INF/classes.lib.resources才是classpath,WEB-INF/是资源目录, 客户端不能直接访问. 2.WEB-INF/class ...
- java项目中classpath路径到底指的是哪里?
本文转自:http://blog.csdn.net/javaloveiphone/article/details/51994268 1.src不是classpath, WEB-INF/classes和 ...
- classpath路径指什么
一.classpath路径指什么 只知道把配置文件如:mybatis.xml.spring-web.xml.applicationContext.xml等放到src目录(就是存放代码.java文件的目 ...
- java代码中获取classpath路径
Javaweb工程中,有时候需要自己手动的去读取classpath下面的配置文件,这里总结一点读取classpath路径的方法,分享一下. 方法一: String path = Test.class. ...
- 《Java项目中classpath路径详解》
项目里用到了classpath路径来引用文件,那么classpath指的是哪里呢 我首先把上面的applicationContext.xml文件放在了src目录下发现可以. 那么classpath到底 ...
- Java获取Window和Linux系统的项目ClassPath路径
不啰嗦,直接复制工具类 /** * 在windows和linux系统下均可正常使用 * Create by yster@foxmail.com 2018/6/6/006 14:51 */ public ...
- 读取指定路径的Properties文件
1.读取项目内的properties文件,项目内的properties文件一般都放在resource文件夹下面, 通过getClassLoader().getResourceAsStream()来获取 ...
随机推荐
- JavaScript中数组迭代方法(jquery)
var arr = [1,2,4,5,6]; //1.forEach(让数组中的每一项做一件事)arr.forEach(function(item,index){ console.log(ite ...
- 遗传算法在JobShop中的应用研究(part 6:结果显示)
def FormatSolution(s, C, I): T = [0 for j in xrange(I.n)] S = [[0 for t in I[j]] for j in xrange(I.n ...
- css伪类的展现
常见的伪类选择器 :link :hover :active :visited 如果为以上几个伪类赋予相同css属性名,不同的css属性值 <!DOCTYPE html> <html ...
- centos7 安装php7+mysql5.7+nginx+redis
.1.先修改yum源 https://webtatic.com rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest- ...
- 常用js方法
function dateGetter(name, size, offset, trim) { offset = offset || 0; return function (date) { var v ...
- Java一个汉字占几个字节(详解与原理)
1.先说重点: 不同的编码格式占字节数是不同的,UTF-8编码下一个中文所占字节也是不确定的,可能是2个.3个.4个字节: 2.以下是源码: @Test public void test1() thr ...
- mysql基本命令整理
1.replace into(insert into 的增强版) replace into tbl_name(col_name, ...) values(...)replace into tbl_na ...
- MFC编程入门之二十四(常用控件:列表框控件ListBox)
前面两节讲了比较常用的按钮控件,并通过按钮控件实例说明了具体用法.本文要讲的是列表框控件(ListBox)及其使用实例. 列表框控件简介 列表框给出了一个选项清单,允许用户从中进行单项或多项选择,被选 ...
- Ajax form表单提交
1. 使用 $("form").serialize() 来获取表单数据 $.ajax({ type: 'post', url: 'your url', data: $(" ...
- angular中动态添加的元素绑定事件问题
$compile http://segmentfault.com/q/1010000000726448/a-1020000000727088 接口下载问题