java web项目中 读取properties 路径的问题
可以先获取项目的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();
}
}
}
java web项目中 读取properties 路径的问题的更多相关文章
- java web项目中 获取resource路径下的文件路径
public GetResource{ String path = GetResource.class.getClassLoader().getResource("xx/xx.txt&quo ...
- Java在Web项目中读取properties文件
import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import ...
- 对Java Web项目中路径的理解
第一个:文件分隔符 坑比Window.window分隔符 用\;unix采用/.于是用File.separator来跨平台 请注意:这是文件路径.在File f = new File(“c:\\hah ...
- (转)关于java和web项目中的相对路径问题
原文:http://blog.csdn.net/yethyeth/article/details/1623283 关于java和web项目中的相对路径问题 分类: java 2007-05-23 22 ...
- JAVA WEB项目中各种路径的获取
JAVA WEB项目中各种路径的获取 标签: java webpath文件路径 2014-02-14 15:04 1746人阅读 评论(0) 收藏 举报 分类: JAVA开发(41) 1.可以在s ...
- linux 下用renameTo方法修改java web项目中文件夹名称问题
经测试,在Linux环境中安装tomcat,然后启动其中的项目,在项目中使用java.io.File.renameTo(File dest)方法可行. 之前在本地运行代码可以修改,然后传到Linux服 ...
- Java Web项目中连接Access数据库的配置方法
本文是对前几天的"JDBC连接Access数据库的几种方式"这篇的升级.因为在做一些小项目的时候遇到的问题,因此才决定写这篇博客的.昨天已经将博客公布了.可是后来经过一些验证有点问 ...
- java web项目中打开资源文件中文乱码
1 java web项目中经常使用多模块管理.在某一个模块中添加了一些资源文件.但不是启动项目.有时候需要在程序中读取资源文件内容,打包后放到容器中就不能正常运行了.需要将所有资源文件放到启动项目的 ...
- 【转】Java Web 项目获取运行时路径 classpath
Java Web 项目获取运行时路径 classpath 假设资源文件放在maven工程的 src/main/resources 资源文件夹下,源码文件放在 src/main/java/下, 那么ja ...
随机推荐
- python3 selenium 鼠标悬停操作
使用场景: 测试过程中有些元素隐藏在某些元素下面,需要鼠标悬停,才会看到 使用方法: # 定位元素hover_element = driver.find_element_by_css_selector ...
- iOS 使用 UIMenuController 且不隐藏键盘的方法
iOS 使用 UIMenuController 且不隐藏键盘的方法 在键盘显示的时候使用 UIMenuController 弹出菜单,保持键盘显示且可输入的状态. 实现方法有 修改响应链(推荐) 遵循 ...
- DCalendar增加月份选择功能--简单jQuery日期选择器插件改动
做时间插件的时候,很多都会遇到要做选择月份的插件,但是DCalendar提供的api只支持日期选择,最近遇到这个问题,所以调整了一下源码,话不多说,先看效果吧 点击日期插件,出现上图,再点击月份就直接 ...
- Netflix Hystrix - 快速入门
Hystrix最初是由Netflix的API team研发的,用于提高API的弹性和性能,2012年在公司内部广受好评. 如果你的应用是一个单独的应用,那几乎不用在意断路的问题. 但在分布式环境中,各 ...
- 添加Metasploit-payload到已有的Android项目中
metasploit在写这篇文章之前,笔者可以说是对java一窍不通,也从来没有写过什么Android应用,在几天的摸爬滚打中终于实现了最终的目的,就是在已有Apk源码的情况下,用了比较另类的方式,添 ...
- Gephi安装
Gephi for mac https://gephi.org/users/download/ 在官网上下载gephi-0.9.1-macos.dmg双击拖到Application里面就好了,注意有的 ...
- Tensorflow中的padding操作
转载请注明出处:http://www.cnblogs.com/willnote/p/6746668.html 图示说明 用一个3x3的网格在一个28x28的图像上做切片并移动 移动到边缘上的时候,如果 ...
- hive的表的基本操作
环境简介 实验环境使用的是cloudera-quickstart-vm-5.0环境. 内容摘要 创建表 修改表名 修改表中的列名 添加列 删除列 替换列 正文 Alter Table 语句 上面所述的 ...
- 【JAVAWEB学习笔记】28_jqueryAjax:json数据结构、jquery的ajax操作和表单校验插件
Ajax-jqueryAjax 今天内容: 1.json数据结构(重点) 2.jquery的ajax操作(重点) 3.jquery的插件使用 一.json数据结构 1.什么是json JSON(J ...
- 你真的了解WebSocket吗?
WebSocket协议是基于TCP的一种新的协议.WebSocket最初在HTML5规范中被引用为TCP连接,作为基于TCP的套接字API的占位符.它实现了浏览器与服务器全双工(full-duplex ...