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 ...
随机推荐
- SDN学习之OpenFlow协议分析
学习SDN相关的学习也已经有快半年了,期间从一无所知到懵懵懂懂,再到现在的有所熟悉,经历了许多,也走了不少弯路,其中,最为忌讳的便是,我在学习过程中,尚未搞明白OpenFlow协议的情况下,便开始对S ...
- LinkedBlockingDeque
1.LinkedBlockingDeque public class LinkedBlockingDeque<E> extends AbstractQueue<E> imple ...
- Android N多窗口支持
Android N 可以同时显示多个应用窗口. 在手机上,两个应用可以在"分屏"模式中左右并排或上下并排显示.例如,用户可以 在上面窗口聊QQ,下面窗口发送短信. 如图所示,两个a ...
- CI集成phpunit Error: No code coverage driver is available 的解决
CI集成phpunit时,运行报No code coverage driver is available的错误,如下图: yanglingdeMacBook-Pro:tests yangling$ p ...
- libsvm参数选择
以前接触过libsvm,现在算在实际的应用中学习 LIBSVM 使用的一般步骤是: 1)按照LIBSVM软件包所要求的格式准备数据集: 2)对数据进行简单的缩放操作: 3)首要考虑选用RBF 核函数: ...
- Swift 中 String 取下标及性能问题
Swift 中 String 取下标及性能问题 取下标 String String 用 String.Index 取下标(subscript)得到 Character,String.Index 要从 ...
- 求一个二维整数数组最大子数组之和,时间复杂度为N^2
本随笔只由于时间原因,我就只写写思想了 二维数组最大子数组之和,可以 引用 一维最大子数组之和 的思想一维最大子数组之和 的思想,在本博客上有,这里就不做多的介绍了 我们有一个最初的二维数组a[n ...
- Unsupervised Learning and Text Mining of Emotion Terms Using R
Unsupervised learning refers to data science approaches that involve learning without a prior knowle ...
- win2012中添加架构FTP服务器
打开IIS管理器(win+R输入inetmgr后回车或通过 添加FTP站点
- [认证授权] 4.OIDC(OpenId Connect)身份认证授权(核心部分)
0 目录 认证授权系列:http://www.cnblogs.com/linianhui/category/929878.html 1 什么是OIDC? 看一下官方的介绍(http://openid. ...