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()来获取 ...
随机推荐
- Canvas 与 Paint 类的 使用
使用canvas画布和paint画笔可以自定义view 案例:fastindexbar 基本用法 public class DrawView extends View{ private Rect mR ...
- Obtaining Query Count Without executing a Query in Oracle D2k
Obtaining Query Count Without executing a Query in Oracle D2k Obtaining a count of records that will ...
- ElasticSearch与Spring Boot集成问题
1.None of the configured nodes are available 或者 org.elasticsearch.transport.RemoteTransportException ...
- miniprofiler对方法的时间性能检测
miniprofiler对方法的时间性能检测 直接上代码 using StackExchange.Profiling; ... var profiler = MiniProfiler.Current; ...
- Chrome谷歌浏览器下不支持css字体小于12px的解决办法
先来看下 ie.火狐.谷歌浏览器下各个字体显示情况 ie下: 火狐下: 谷歌下: 从上面的图可以很明显看出谷歌下 css设置字体大小为12px及以下时,显示都是一样大小,都是默认12px; 那么网上一 ...
- (十二)select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
select函数用于在非阻塞中,当一个套接字或一组套接字有信号时通知你,系统提供select函数来实现多路复用输入/输出模型,原型:int select(int maxfd,fd_set *rdset ...
- MFC编程入门之二十(常用控件:静态文本框)
上一节讲了颜色对话框之后,关于对话框的使用和各种通用对话框的介绍就到此为止了.从本节开始将讲解各种常用控件的用法.常用控件主要包括:静态文本框.编辑框.单选按钮.复选框.分组框.列表框.组合框.图片控 ...
- 转:OSGi 入门篇:模块层
OSGi 入门篇:模块层 1 什么是模块化 模块层是OSGi框架中最基础的一部分,其中Java的模块化特性在这一层得到了很好的实现.但是这种实现与Java本身现有的一些模块化特性又有明显的不同. 本文 ...
- django 模型
一.project 与app之间的关系 1个project中可包含多个app eg:包含两个app的project的结构
- 【OS】实模式和保护模式区别及寻址方式
实模式和保护模式区别及寻址方式 转载请注明出处:http://blog.csdn.NET/rosetta 64KB-4GB-64TB? 我记得大学的汇编课程.组成原理课里老师讲过实模式和保护模式的区别 ...