在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的更多相关文章

  1. web classpath 路径说明

    classpath路径在每个J2ee项目中都会用到,即WEB-INF下面的classes目录,所有src目录下面的java.xml.properties等文件编译后都会在此,所以在开发时常将相应的xm ...

  2. classpath 及读取 properties 文件

    java代码中获取项目的静态文件,如获取 properties 文件内容是必不可少的. Spring 下只需要通过 @Value 获取配置文件值 <!-- 资源文件--> <util ...

  3. Java项目中classpath路径

    1.src不是classpath, WEB-INF/classes.lib.resources才是classpath,WEB-INF/是资源目录, 客户端不能直接访问. 2.WEB-INF/class ...

  4. java项目中classpath路径到底指的是哪里?

    本文转自:http://blog.csdn.net/javaloveiphone/article/details/51994268 1.src不是classpath, WEB-INF/classes和 ...

  5. classpath路径指什么

    一.classpath路径指什么 只知道把配置文件如:mybatis.xml.spring-web.xml.applicationContext.xml等放到src目录(就是存放代码.java文件的目 ...

  6. java代码中获取classpath路径

    Javaweb工程中,有时候需要自己手动的去读取classpath下面的配置文件,这里总结一点读取classpath路径的方法,分享一下. 方法一: String path = Test.class. ...

  7. 《Java项目中classpath路径详解》

    项目里用到了classpath路径来引用文件,那么classpath指的是哪里呢 我首先把上面的applicationContext.xml文件放在了src目录下发现可以. 那么classpath到底 ...

  8. Java获取Window和Linux系统的项目ClassPath路径

    不啰嗦,直接复制工具类 /** * 在windows和linux系统下均可正常使用 * Create by yster@foxmail.com 2018/6/6/006 14:51 */ public ...

  9. 读取指定路径的Properties文件

    1.读取项目内的properties文件,项目内的properties文件一般都放在resource文件夹下面, 通过getClassLoader().getResourceAsStream()来获取 ...

随机推荐

  1. [OC][地图] 高德地图之定位初探(一)

    使用前的说明 高德地图开放平台的iOS定位模块网址-->http://lbs.amap.com/api/ios-location-sdk/summary/ 高德地图有Web端.android平台 ...

  2. TensorFlow支持windows了

    (留坑)找个时间测试一下. 终于来了,TensorFlow 新增官方 Windows 支持

  3. web前端性能优化指南(转)

    web前端性能优化指南 概述 1. PC优化手段在Mobile侧同样适用2. 在Mobile侧我们提出三秒种渲染完成首屏指标3. 基于第二点,首屏加载3秒完成或使用Loading4. 基于联通3G网络 ...

  4. js获取当前日期

    var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();    //获取完整的年份(4位,1 ...

  5. File API 读取上传的文件

    1, 在html 文档中,<input type="file"> 我们可以选择文件进行上传,但这时只能上传一个文件.如果加上multiple 属性,可以上传多个文件,上 ...

  6. strcpy strlen memcpy等的函数实现

    #include <assert.h> #include <string.h> #include <stdlib.h> #include <stdio.h&g ...

  7. [阅读笔记]Software optimization resources

    http://www.agner.org/optimize/#manuals 阅读笔记Optimizing software in C++   7. The efficiency of differe ...

  8. javascript的假查询

    1. function select(){ var text=$("#ss").val();//获得关键字 $("#show_tab tr").hide().f ...

  9. SPSS数据分析—广义估计方程

    广义线性模型虽然很大程度上拓展了线性模型的应用范围,但是其还是有一些限制条件的,比如因变量要求独立,如果碰到重复测 量数据这种因变量不独立的情况,广义线性模型就不再适用了,此时我们需要使用的是广义估计 ...

  10. css学习笔记 5

    将css引入到html页面中的方法: 用style属性设置样式 用<style>标签设置样式 用<link>标签引入外部样式文件 用@import引入外部样式文件 <li ...