可以先获取项目的classPath

String classPath = this.getClass().getResource("/").getPath();//获取classPath(部署到tomcat的路径上)

我的为/D:/apache-tomcat-6.0.29/webapps/demo/WEB-INF/classes/  在连接下面的路径即可

代码如下:

package readproperties;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.Enumeration;
import java.util.Properties;

public class readProperties {
public static void main(String[] args) {
readProperty r = new readProperty();
r.read();
}

public void read(){
readProperty r = new readProperty();
r.read();
r.add();
}

}

class readProperty{
public void read(){
//readProperties rp = new readProperties();
//获取classpath
String classPath = this.getClass().getResource("/").getPath();//获取classPath(部署到tomcat的路径上)
String filepath = classPath+"readProperties/test.properties";
Properties p = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(filepath));
//InputStream in = this.getClass().getClassLoader().getResourceAsStream("test.properties");
p.load(in);
//读取单个的信息

String value = p.getProperty("sex");
System.out.println(value);

//读取所有的信息
Enumeration<?> en = p.propertyNames();
while(en.hasMoreElements()){
String key = (String)en.nextElement();
System.out.println(key+"==="+p.getProperty(key));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void add(){
String classPath = this.getClass().getResource("/").getPath();
String filepath = classPath+"readProperties/test.properties";
Properties p = new Properties();
InputStream in; //
try {
in = new BufferedInputStream(new FileInputStream(filepath));
p.load(in);
//FileOutputStream out = new FileOutputStream(new File(filepath));

Writer w = new FileWriter(new File(filepath));

p.setProperty("key2", "value2");
p.setProperty("habbit2", "ball2");
//以适合使用 load 方法加载到 Properties 表中的格式,
//将此 Properties 表中的属性列表(键和元素对)写入输出流
p.store(w, "");

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

最新文章

  1. 灰色预测原理及JAVA实现
  2. Echarts-画堆积柱状图
  3. Oracle BFILE备忘
  4. 浅析WINFORM工具条的重用实现
  5. display:inline 跟 display:block 跟 display:inline-block区别
  6. 线性链表的双向链表——java实现
  7. [Android] Upload package to device fails #2720
  8. IOS学习7——cocoapod安装与使用教程
  9. 【实战小项目】python开发自动化运维工具--批量操作主机
  10. 3DMath
  11. 箭头函数里this理解
  12. 21-json pickle shelve XML
  13. Linux部署Web应用程序超链接下载中文名称文件404问题解决办法
  14. 转载:SDWebImage支持URL不变时更新图片内容
  15. jQuery图片延迟加载插件:jquery.lazyload
  16. POJ 2247
  17. Jquery Mobile实例--利用优酷JSON接口读取视频数据
  18. 并发时-修改Linux系统下的最大文件描述符限制
  19. go get 下载需要的相关工具
  20. 打杂程序员之nginx服务配置

热门文章

  1. Python实现脚本锁功能,同时只能执行一个脚本
  2. dp
  3. JDK安装以及安装过程中出现的问题(日志二)
  4. 解析Excel文件并把数据存入数据库
  5. 如何通过 WebP 自适应方案减少图片资源大小
  6. [刷题]算法竞赛入门经典(第2版) 5-16/UVa212 - Use of Hospital Facilities
  7. springboot(三):Spring boot中Redis的使用
  8. 学生成绩管理C语言版
  9. PHP二位数组按照数组的某个字段值排序
  10. struts2.1.6教程二、struts.xml配置及例程