问一个读取属性文件的问题:
Test.properties文件有如下内容:
name         valueadd          AddCart想获取key为add的内容,即AddCart,方法如下
public class CommandProperties { private Properties pro;

public String getCommandName(String key){
InputStream fis=CommandProperties.class.getResourceAsStream("Test.properties");
pro=new Properties();
try {
pro.load(fis);
} catch (IOException e) {
e.printStackTrace();
}
return pro.getProperty(key);
}
}为什么测试中发现返回的是null?方法是在网上找的,求高人指点!

解决方案 »

  1.   

    +1
    2.确保你的Test.properties在classpath下(src),如果不是的话需要添加完整包名。
      

  2.   

    import java.util.Properties;
     public boolean loadPropFile(String sPropFileName) {
            File file = new File(sPropFileName);
            try {
                FileInputStream in = new FileInputStream(file);
                properties = new Properties();
                properties.load(in);
                in.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
        /**
         * Get property value
         * 
         * @param key
         *            String : properties name
         * @return String : properties value
         */    public String getPropValue(String key) {        // If Key is null or equals blank, return "".
            if (null == key || key.equals("")) {
                return "";
            }        String value = getStr(properties.getProperty(key));
            try {
                // For Chinese word, double byte conversion.
                value = new String(value.getBytes("8859_1"), "GB2312");
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
                value = "";
            }
            return value;    }    /**
         * String convertion function.
         * 
         * @param pInStr
         *            the string to be converted
         * @return the converted string
         */    private String getStr(String pInStr) {
            return null == pInStr ? "" : pInStr.trim();
        }用这个试试,还有就是注意文件路径。你那KEY有对应值吗.下班了,go........
      

  3.   


    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;public class CommandProperties {
    private Properties pro; public String getCommandName(String key) {
    InputStream fis;
    try {
    fis = new FileInputStream("Test.properties");
    pro = new Properties();
    pro.load(fis);
    } catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace(); } catch (IOException e) {
    e.printStackTrace();
    }
    return pro.getProperty(key);
    } public static void main(String[] args) {
    String result = new CommandProperties().getCommandName("name");
    System.out.println("result=" + result);
    }}这个没有问题,我试过了,使用FileInputStream
      

  4.   


    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;public class CommandProperties {
    private Properties pro; public String getCommandName(String key) {
    InputStream fis;
    try {
    fis = new FileInputStream("config/Test.properties");
    pro = new Properties();
    pro.load(fis);
    } catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace(); } catch (IOException e) {
    e.printStackTrace();
    }
    return pro.getProperty(key);
    } public static void main(String[] args) {
    String result = new CommandProperties().getCommandName("name");
    System.out.println("result=" + result);
    }}
    这个没有问题,我试过了,使用FileInputStream
      

  5.   

    在属性文件中应该是name=value  add=AddCart,中间少了等号