我有一个web项目,在同一个包下有一个类里要读取对应的.properties文件,该怎么来表示其读取路径才能正确的读取出来这个文件?
我试了很多种了都不行!

解决方案 »

  1.   

    都放classes下就可以了,路径就直接用该文件
      

  2.   

    // FrontEnd Plus GUI for JAD
    // DeCompiled : PropertyManager.class
    package tools;
    import java.io.*;
    import java.util.*;public class PropertyManager { private static PropertyManager manager = null; private static Object managerLock = new Object(); private static String propsName = "/elearning.properties"; private Properties properties; private Object propertiesLock; private String resourceURI; public PropertyManager() {
    properties = null;
    propertiesLock = new Object();
    } public static String getProperty(String s) {
    if (manager == null)
    synchronized (managerLock) {
    if (manager == null)
    manager = new PropertyManager(propsName);
    }
    return manager.getProp(s);
    } public static void setProperty(String s, String s1) {
    if (manager == null)
    synchronized (managerLock) {
    if (manager == null)
    manager = new PropertyManager(propsName);
    }
    manager.setProp(s, s1);
    } public static void deleteProperty(String s) {
    if (manager == null)
    synchronized (managerLock) {
    if (manager == null)
    manager = new PropertyManager(propsName);
    }
    manager.deleteProp(s);
    } public static Enumeration propertyNames() {
    if (manager == null)
    synchronized (managerLock) {
    if (manager == null)
    manager = new PropertyManager(propsName);
    }
    return manager.propNames();
    } public static boolean propertyFileIsReadable() {
    if (manager == null)
    synchronized (managerLock) {
    if (manager == null)
    manager = new PropertyManager(propsName);
    }
    return manager.propFileIsReadable();
    } public static boolean propertyFileIsWritable() {
    if (manager == null)
    synchronized (managerLock) {
    if (manager == null)
    manager = new PropertyManager(propsName);
    }
    return manager.propFileIsWritable();
    } public static boolean propertyFileExists() {
    if (manager == null)
    synchronized (managerLock) {
    if (manager == null)
    manager = new PropertyManager(propsName);
    }
    return manager.propFileExists();
    } private PropertyManager(String s) {
    properties = null;
    propertiesLock = new Object();
    resourceURI = s;
    } protected String getProp(String s) {
    if (properties == null)
    synchronized (propertiesLock) {
    if (properties == null)
    loadProps();
    }
    String s1 = properties.getProperty(s);
    if (s1 == null)
    return null;
    else
    return s1.trim();
    } protected void setProp(String s, String s1) {
    synchronized (propertiesLock) {
    if (properties == null)
    loadProps();
    properties.setProperty(s, s1);
    saveProps();
    }
    } protected void deleteProp(String s) {
    synchronized (propertiesLock) {
    if (properties == null)
    loadProps();
    properties.remove(s);
    saveProps();
    }
    } protected Enumeration propNames() {
    if (properties == null)
    synchronized (propertiesLock) {
    if (properties == null)
    loadProps();
    }
    return properties.propertyNames();
    } private void loadProps() {
    properties = new Properties();
    InputStream inputstream = null;
    try {
    inputstream = getClass().getResourceAsStream(resourceURI);
    properties.load(inputstream);
    } catch (Exception exception) {
    exception.printStackTrace();
    } finally {
    try {
    inputstream.close();
    } catch (Exception exception2) {
    }
    }
    } private void saveProps() {
    String s = properties.getProperty("path").trim();
    FileOutputStream fileoutputstream = null;
    try {
    fileoutputstream = new FileOutputStream(s);
    properties.store(fileoutputstream, "elearning.properties -- "
    + new Date());
    } catch (Exception exception) {
    exception.printStackTrace();
    } finally {
    try {
    fileoutputstream.close();
    } catch (Exception exception2) {
    }
    }
    } public boolean propFileIsReadable() {
    try {
    InputStream inputstream = getClass().getResourceAsStream(
    resourceURI);
    return true;
    } catch (Exception exception) {
    return false;
    }
    } public boolean propFileExists() {
    /*
     * String s = getProp("path"); if (s == null) return false; File file =
     * new File(s); return file.isFile();
     */
    return true;
    } public boolean propFileIsWritable() {
    String s = getProp("path");
    File file = new File(s);
    if (file.isFile())
    return file.canWrite();
    else
    return false;
    }

    public static void main(String[] args) {
    System.out.println(PropertyManager.getProperty("DEFAULTALIAS_2"));
    }}
      

  3.   

    楼上写那么多!!!String tomcat_path = System.getProperty("user.dir").replace("\\", "/") +"/../webapps/项目名称";//这个方法可以取得tomcat下你的项目的绝对路径
      

  4.   

    接着上面的File file = new  File(tomcat_path+"你的文件名");
      

  5.   

    包名:com.branch.common,DBPoolResources.properties是配置文件,放在com.branch.common包下。
    配置文件内容:
    db.driver  = com.microsoft.jdbc.sqlserver.SQLServerDriver程序中:
    import java.util.ResourceBundle;public static ResourceBundle apps=ResourceBundle.getBundle("com.branch.common.DBPoolResources");
    driver=apps.getString("db.driver");
      

  6.   

    我的路径是这样的com/mps/uc/tradeandhint/dao/configure.properties
    在这个com/mps/uc/tradeandhint/dao/a.class
    我要在这个a.class里如何读取到那个configure.properties。
      

  7.   

    在a.class里:
    import java.util.ResourceBundle; 
    public static ResourceBundle apps=ResourceBundle.getBundle("com/mps/uc/tradeandhint/dao/configure"); 
    driver=apps.getString("db.driver");这样就可以了。configure.properties里面:
    db.driver=.....