可以先获取项目的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 路径的问题的更多相关文章

  1. java web项目中 获取resource路径下的文件路径

    public GetResource{ String path = GetResource.class.getClassLoader().getResource("xx/xx.txt&quo ...

  2. Java在Web项目中读取properties文件

    import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import ...

  3. 对Java Web项目中路径的理解

    第一个:文件分隔符 坑比Window.window分隔符 用\;unix采用/.于是用File.separator来跨平台 请注意:这是文件路径.在File f = new File(“c:\\hah ...

  4. (转)关于java和web项目中的相对路径问题

    原文:http://blog.csdn.net/yethyeth/article/details/1623283 关于java和web项目中的相对路径问题 分类: java 2007-05-23 22 ...

  5. JAVA WEB项目中各种路径的获取

    JAVA WEB项目中各种路径的获取 标签: java webpath文件路径 2014-02-14 15:04 1746人阅读 评论(0) 收藏 举报  分类: JAVA开发(41)  1.可以在s ...

  6. linux 下用renameTo方法修改java web项目中文件夹名称问题

    经测试,在Linux环境中安装tomcat,然后启动其中的项目,在项目中使用java.io.File.renameTo(File dest)方法可行. 之前在本地运行代码可以修改,然后传到Linux服 ...

  7. Java Web项目中连接Access数据库的配置方法

    本文是对前几天的"JDBC连接Access数据库的几种方式"这篇的升级.因为在做一些小项目的时候遇到的问题,因此才决定写这篇博客的.昨天已经将博客公布了.可是后来经过一些验证有点问 ...

  8. java web项目中打开资源文件中文乱码

    1 java web项目中经常使用多模块管理.在某一个模块中添加了一些资源文件.但不是启动项目.有时候需要在程序中读取资源文件内容,打包后放到容器中就不能正常运行了.需要将所有资源文件放到启动项目的 ...

  9. 【转】Java Web 项目获取运行时路径 classpath

    Java Web 项目获取运行时路径 classpath 假设资源文件放在maven工程的 src/main/resources 资源文件夹下,源码文件放在 src/main/java/下, 那么ja ...

随机推荐

  1. oracle AWR性能监控报告生成方法

    目前相当一部分公司会用到oracle,在做性能测试的时候,对数据库的监控很重要,那么这里先介绍下如何生成oracle自带的awr监控报告,而具体报告的内容分析会放在后续的博客中 oracle性能分析入 ...

  2. linux服务器下安装node

    在百度上搜了好久,都没有完整的答案,好多都已经过时了!特留下此脚印 # 检查是否已经安装pythonrpm -qa | grep python# 查版本python# 最好是重新安装 Python推荐 ...

  3. JavaScript利用闭包循环绑定事件

    我们经常在做前端面试题的时候,会遇到循环绑定事件后,输出打印结果,很多人总是搞不清楚,今天借此机会跟大家梳理一下闭包相关作用. 1.首先我们举一个简单的例子. html部分: <a href=& ...

  4. 【WPF】获取电磁笔的压感

    WPF 不仅支持触控,也支持笔的输入,比如现在比较高大上的电磁笔.便宜的板子一般不配备电磁笔,而是配电容笔,虽然也号称XXX级压感,但是效果自然不可与电磁笔相比. UIElement 类规范了UI元素 ...

  5. zen coding一个牛的不行的html和css开发工具

    zen coding 是一种仿css选择器的语法来快速开发html和css的开源项目.现已更名为Emmet.可以到github上下载拜读.在这个都想偷懒的世界里,此方法可以极大的缩短开发人员的开发时间 ...

  6. Kafka学习-简介

      Kafka是由LinkedIn开发的一个分布式的消息系统,使用Scala编写,它以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如Cloudera.Apache Storm.S ...

  7. gitlab 添加SSH Key

    1.登录http://domain/users/sign_in 2.选择"Profile Settings",进入"Profile Settings"设置页面 ...

  8. php.ini 文件中配置的意义注释

    ;;;;;;;;;;;;;;;;;;;; About php.ini   ;  //关于php;;;;;;;;;;;;;;;;;;;; PHP's initialization file, gener ...

  9. Vue之Vuex

    一.什么是vuex Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.简单来说就是一个数据统一 ...

  10. Managing Spark data handles in R

    When working with big data with R (say, using Spark and sparklyr) we have found it very convenient t ...