zoukankan      html  css  js  c++  java
  • 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();
    }

    }
    }

  • 相关阅读:
    HTTP缓存总结
    最近公司用到了lombok,感觉很不错的样子,所以上网搜了一些资料,总结了一下用法。
    servlet中获取各种相对地址(服务器、服务器所在本地磁盘、src等)。
    centos 7 lvm 扩容刷新lv 报错Couldn't find valid filesystem superblock.的解决
    自动化运维工具——ansible详解(二)
    自动化运维工具——ansible详解(一)
    k8s中删除pod的操作
    rsync 只同步目录结构 不同步文件
    redis-info命令输出详解
    centos解决“c++: internal compiler error: Killed (program cc1plus)”错误
  • 原文地址:https://www.cnblogs.com/lilefordream/p/3921560.html
Copyright © 2011-2022 走看看