Java ClassLoader loadPropertiesFromFile(File parent)

Here you can find the source of loadPropertiesFromFile(File parent)

Description

load Properties From File

License

Apache License

Declaration

private static void loadPropertiesFromFile(File parent) throws FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Main {
    static boolean inited;
    final static Properties allProperties = new Properties();
    static Method envHelperGetPropertiesMethod;

    private static void loadPropertiesFromFile(File parent) throws FileNotFoundException, IOException {
        File[] files = parent.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                loadPropertiesFromFile(file);
            } else {
                String path = file.getPath();
                if (path.endsWith("properties")) {
                    if (path.contains("i18n")) {
                        continue;
                    }/*from w ww  .j  a  va 2  s  . c om*/
                    Properties p = new Properties();
                    p.load(new FileReader(file));
                    allProperties.putAll(p);
                    System.out.println("load properties from file:" + path);
                }
            }
        }
    }

    private synchronized static void load() {
        try {
            if (inited) {
                return;
            }
            try {
                Class<?> threadClazz = Class.forName("cn.com.warlock.spring.helper.EnvironmentHelper");
                envHelperGetPropertiesMethod = threadClazz.getMethod("getProperty", String.class);
            } catch (Exception e) {
            }
            URL url = Thread.currentThread().getContextClassLoader().getResource("");
            if (url.getProtocol().equals("file")) {
                loadPropertiesFromFile(new File(url.getPath()));
            } else if (url.getProtocol().equals("jar")) {
                try {
                    String jarFilePath = url.getFile();
                    jarFilePath = jarFilePath.split("jar!")[0] + "jar";
                    jarFilePath = jarFilePath.substring("file:".length());
                    jarFilePath = java.net.URLDecoder.decode(jarFilePath, "UTF-8");
                    JarFile jarFile = new JarFile(jarFilePath);

                    Enumeration<JarEntry> entries = jarFile.entries();
                    while (entries.hasMoreElements()) {
                        JarEntry entry = entries.nextElement();
                        if (entry.getName().endsWith(".properties")) {
                            InputStream inputStream = jarFile.getInputStream(jarFile.getJarEntry(entry.getName()));
                            Properties properties = new Properties();
                            properties.load(inputStream);
                            try {
                                inputStream.close();
                            } catch (Exception e) {
                            }
                            allProperties.putAll(properties);
                        }

                    }
                    jarFile.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
            inited = true;
        } catch (Exception e) {
            inited = true;
            throw new RuntimeException(e);
        }
    }
}

Related

  1. loadGuiConfiguration(String configurationFilename)
  2. loadProperties(File inFile)
  3. loadProperties(final String configFile, final String chainProperty)
  4. loadProperties(final String location)
  5. loadProperties(String propFile)
  6. loadTextFile(final String location)
  7. loadXml()
  8. locateFile(String name)
  9. read(String fileName)