java 传key值读取properties数据value值

public class Conf {

private static final Properties prop = new Properties();

String path = Conf.class.getClassLoader().getResource("config").getPath();

File file = new File(path);

if(file.isDirectory()) {
File[] files = file.listFiles();
for(File f:files) {
if(f.isFile()) {
        String fileName = f.getName();
        String[] names = fileName.split("\\.");
        if(names.length==-1 || !"properties".equals(names[names.length-1])) {
     return ;
         }
       BufferedReader beader = null;
       Properties pop = new Properties();
       try {
     beader = new BufferedReader(new InputStreamReader(new FileInputStream(f),"UTF-8"));
    pop.load(beader);
      } catch (Exception e) {
     if(beader!=null) {
     try {
beader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
    }
        }
     for (Object key : pop.keySet()) {
    String value = pop.getProperty(key.toString());
    prop.put(key, value);
    
}
}

System.out.println(prop.getProperty("key"));//传入key 就可以输出value值了

}

你可能感兴趣的:(java 传key值读取properties数据value值)