public static String read(String fileName,String key) {
FileInputStream inputFile = null;
String filePath = null;
Properties pro = new Properties();
try {
filePath = new String(URLDecoder.decode(PropertyUtil.class
.getClassLoader().getResource(fileName).getPath(),
"UTF-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("-----------"+filePath);
  try {
             String value = "";
             inputFile = new FileInputStream(filePath);
             pro.load(inputFile);
            
             inputFile.close();
             if(pro.contains(key)){
                 value = pro.getProperty(key);
                 return value;
             }else{
                 return "";
             }
         } catch (FileNotFoundException e) {
             e.printStackTrace();
             return "";
         } catch (IOException e) {
             e.printStackTrace();
             return "";
         } catch (Exception e) {
             e.printStackTrace();
             return "";
         }
}求大虾们帮忙看看那

解决方案 »

  1.   

    package com.accp.test;import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;public class PropertyUtil { public static void main(String[] args){
    System.out.println(read("aa.properties","aa"));
    }

    public static String read(String fileName, String key) {
    InputStream inputFile = null;
    String filePath = null;
    Properties pro = new Properties();
    try {
    filePath = PropertyUtil.class.getResource(fileName).getPath();
    System.out.println("-----------" + filePath);
    inputFile = new FileInputStream(new File(PropertyUtil.class.getResource(fileName).getPath()));
    } catch (Exception e1) {
    e1.printStackTrace();
    }
    try {
    String value = "";
    pro.load(inputFile);
    inputFile.close();
    if (pro.containsKey(key)) {
    value = pro.getProperty(key);
    return value;
    } else {
    return "";
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    return "";
    } catch (IOException e) {
    e.printStackTrace();
    return "";
    } catch (Exception e) {
    e.printStackTrace();
    return "";
    }
    }}
      

  2.   

    debug 最简单了或在
    filePath = new String(URLDecoder.decode(PropertyUtil.class.getClassLoader().getResource(fileName).getPath(),"UTF-8"));的前面加一句:
    System.out.println(fileName.getAbsolutePath());看看fileName的绝对路径对不对 如果不对 照着改就行了
      

  3.   

    不小心看错了 应该是
    System.out.println(new File(fileName).getAbsolutePath());
      

  4.   

    ResourceBundle bundle = ResourceBunder.getBundle("context-adms");
    String ipLibPath = bundle.getString("adms.iplib.path");
      

  5.   

    package com.accp.test;import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;public class PropertyUtil { public static void main(String[] args){
    System.out.println(read("aa.properties","aa"));
    }

    public static String read(String fileName, String key) {
    InputStream inputFile = null;
    String filePath = null;
    Properties pro = new Properties();
    try {
    filePath = PropertyUtil.class.getResource(fileName).getPath();
    System.out.println("-----------" + filePath);
    inputFile = new FileInputStream(new File(PropertyUtil.class.getResource(fileName).getPath()));
    } catch (Exception e1) {
    e1.printStackTrace();
    }
    try {
    String value = "";
    pro.load(inputFile);
    inputFile.close();
    if (pro.containsKey(key)) {
    value = pro.getProperty(key);
    return value;
    } else {
    return "";
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    return "";
    } catch (IOException e) {
    e.printStackTrace();
    return "";
    } catch (Exception e) {
    e.printStackTrace();
    return "";
    }
    }}