`
liuxi1024
  • 浏览: 384022 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

路径问题--web项目中读写properties文件

阅读更多

1、web项目中读properties文件,一般放到class根目录下web/WEB-INF/classes/

public static String FILE_PATH = "/comms.properties";
private InputStream inputFile;
private Properties propertie;

propertie = new Properties();
inputFile = getClass().getResourceAsStream(FILE_PATH);
propertie.load(inputFile);
inputFile.close();

 

2、向properties文件写入时,需要获取绝对路径才能写入操作

public void saveFile(String fileName, String description)
    {
    	String filePath = getClass().getResource("/").getPath()+fileName;
    	File file = new File(filePath);  
        try {
            outputFile = new FileOutputStream(file);
            propertie.store(outputFile, description);
            outputFile.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ioe){
            ioe.printStackTrace();
        }
    }

 getClass().getResource("/").getPath() 为获取class的根目录

 

3、不过在web服务启动中,经常变动*.properties这种方式会重新加载web工程

package com.mts.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 读取properties文件
 *
 */
public class PropertiesUtils
{
    private Properties propertie;
    private InputStream inputFile;
    private FileOutputStream outputFile;
    
    public static String FILE_PATH = "/mts.properties";
    
    /**
     * 初始化Configuration类
     */
    public PropertiesUtils()
    {
        propertie = new Properties();
    }
    
    /**
     * 初始化Configuration类
     * @param filePath 要读取的配置文件的路径+名称
     */
    public PropertiesUtils(String filePath)
    {
        propertie = new Properties();
        try {
        	inputFile = getClass().getResourceAsStream(filePath); 
            propertie.load(inputFile);
            inputFile.close();
        } catch (FileNotFoundException ex) {
            System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
            ex.printStackTrace();
        } catch (IOException ex) {
            System.out.println("装载文件--->失败!");
            ex.printStackTrace();
        }
    }//end ReadConfigInfo(...)
    
    /**
     * 重载函数,得到key的值
     * @param key 取得其值的键
     * @return key的值
     */
    public String getValue(String key)
    {
        if(propertie.containsKey(key)){
            String value = propertie.getProperty(key);//得到某一属性的值
            return value;
        }
        else 
            return "";
    }//end getValue(...)
    
    /**
     * 重载函数,得到key的值
     * @param fileName properties文件的路径+文件名
     * @param key 取得其值的键
     * @return key的值
     */
    public String getValue(String fileName, String key)
    {
        try {
            String value = "";
            inputFile = getClass().getResourceAsStream(fileName);
            propertie.load(inputFile);
            inputFile.close();
            
            if(propertie.containsKey(key)){
                value = propertie.getProperty(key);
                return value;
            }else
                return value;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "";
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        } catch (Exception ex) {
            ex.printStackTrace();
            return "";
        }
    }//end getValue(...)
    
    /**
     * 清除properties文件中所有的key和其值
     */
    public void clear()
    {
        propertie.clear();
    }//end clear();
    
    /**
     * 改变或添加一个key的值,当key存在于properties文件中时该key的值被value所代替,
     * 当key不存在时,该key的值是value
     * @param key 要存入的键
     * @param value 要存入的值
     */
    public void setValue(String key, String value)
    {
        propertie.setProperty(key, value);
    }//end setValue(...)
    
    /**
     * 将更改后的文件数据存入指定的文件中,该文件可以事先不存在。
     * @param fileName 文件路径+文件名称
     * @param description 对该文件的描述
     */
	public void saveFile(String fileName, String description) {
		String filePath = getClass().getResource("/").getPath() + fileName;
		File file = new File(filePath);
		try {
			outputFile = new FileOutputStream(file);
			propertie.store(outputFile, description);
			outputFile.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	} 

}

 

分享到:
评论
2 楼 ghhpig 2010-12-01  
小问题请假下,为什么我的默认路径是在eclipse的安装目录下的?如何解决呢?谢谢
1 楼 zhangyuqing052 2010-04-12  
propertie.store(outputFile, description);
为什么往properties文件中写时会出现#开头?
Ex:
propertie.store(outputFile, "name=liuxi1024");

打开文件的结果是: #name = liuxi1024
帮忙解释一下,谢谢!

还有就是,怎么样直接往properties文件中写Map,需要自己重写方法吗?

相关推荐

    asp.net知识库

    帮助解决网页和JS文件中的中文编码问题的小工具 慎用const关键字 装箱,拆箱以及反射 动态调用对象的属性和方法——性能和灵活性兼备的方法 消除由try/catch语句带来的warning 微软的应试题完整版(附答案) 一个...

    单点登录源码

    基于bootstrap实现的响应式Material Design风格的通用后台管理系统,`zheng`项目所有后台系统都是使用该模块界面作为前端展示。 > zheng-ui 各个子系统前台thymeleaf模板,前端资源模块,使用nginx代理,实现动静...

    ASP.NET2.0高级编程(第4版)1/6

    22.2 读写文件764 22.2.1 流764 22.2.2 Reader和Writer769 22.2.3 压缩流773 22.3 处理串行端口779 22.4 网络通信779 22.4.1 WebRequest和  WebResponse779 22.4.2 发送邮件786 22.5 小结787 第23章 用户控件、...

    2.ASP.NET.2.0.高级编程(第4版) [1/7]

    6.3.5 把文件内容从Stream对象移动到Byte数组中 175 6.4 MultiView和View服务器控件 175 6.5 Wizard服务器控件 179 6.5.1 定制边栏导航 181 6.5.2 AllowReturn属性 182 6.5.3 使用StepType属性 182 6.5.4 给...

    java 面试题 总结

    动态INCLUDE用jsp:include动作实现 它总是会检查所含文件中的变化,适合用于包含动态页面,并且可以带参数。 静态INCLUDE用include伪码实现,定不会检查所含文件的变化,适用于包含静态页面...

    SmartWx微信公众号管理系统-其他

    upload.properties 如属性文件所描述,如图片想放到项目中,res.upload.url注释即可 7、缓存设置。为兼容jdk1.7,此版本中暂时废弃j2cache,默认仅使用ehcache缓存,如想使用,修改J2CacheUtil,用CacheJ2Utils替换...

    Nginx安装包

    启动项目,直接在浏览器中输入个人中心的路径,localhost:8081/userCenter。可以看到没报任何空指针错误,直接就进入了个人中心页面(当然要在同个浏览器中哦)。共享成功。 随后打开我们上次配置好的nginx,进入...

    iBATIS实战

    本书既可为广大的开发人员(不仅仅是Web应用程序开发人员)提供指导,也可为架构师的项目决策提供参考。项目经理、数据库管理员、质量保证员与测试员以及系统分析师也能从本书中受益。 目录: 第一部分 介绍 第1章 ...

    超级有影响力霸气的Java面试题大全文档

     动态INCLUDE用jsp:include动作实现 它总是会检查所含文件中的变化,适合用于包含动态页面,并且可以带参数。 静态INCLUDE用include伪码实现,定不会检查所含文件的变化,适用于包含静态页面...

    JMeter操作手册大全.docx

    jmeter.properties:Jmeter配置文件 jmeter-server.bat:windows下启动负载生成器服务文件 jmeter-server:Linux下启动负载生成器文件 /docs目录——Jmeter帮助文档 /extras目录——提供了对Ant的支持文件,可也用于...

Global site tag (gtag.js) - Google Analytics