Java ClassLoader loadProperties(File inFile)

Here you can find the source of loadProperties(File inFile)

Description

load Properties

License

Open Source License

Declaration

public static Properties loadProperties(File inFile) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;

import java.io.Flushable;
import java.io.IOException;
import java.io.InputStream;

import java.net.URL;

import java.util.Arrays;

import java.util.Properties;

import java.util.function.Consumer;

import java.util.function.Predicate;

import java.util.stream.Stream;

public class Main {
    public static final boolean NEVER_CLOSE_STANDARD_STREAMS = true;

    public static Properties loadProperties(File inFile) throws IOException {

        final Properties p = new Properties();

        try (InputStream theIs = new FileInputStream(inFile)) {

            p.load(theIs);//from   w ww  .  j  av  a2 s  . c  o  m
        }

        return p;
    }

    public static Properties loadProperties(String inResource) throws IOException {

        final InputStream theIs = getResource(inResource);

        if (theIs == null) {
            return null;
        }

        final Properties p = new Properties();

        try {
            p.load(theIs);
        } finally {
            close(theIs);
        }

        return p;
    }

    /**
     * 
     * trying to fetch data from the following resources
     * 
     * CLASSPATH,
     * FILE,
     * URL
     * 
     * @param inResource
     * @return
     */
    public static InputStream getResource(String inResource) {
        return getResource(inResource, true);
    }

    public static InputStream getResource(String inResource, final boolean inAllowUrlResolution) {

        InputStream theIs = null;

        try {

            theIs = inResource == null ? null : getResourceUrl(inResource, inAllowUrlResolution).openStream();

        } catch (Exception e) {
            // swallow
        }

        return theIs;

    }

    public static void close(Closeable... inCloseable) {
        if (inCloseable == null) {
            return;
        }
        Arrays.stream(inCloseable).forEach((c) -> {
            close(c, false);
        });
    }

    public static void close(Closeable inCloseable, boolean inFlush) {

        if (inFlush && inCloseable instanceof Flushable) {
            flush((Flushable) inCloseable);
        }

        try {

            if (NEVER_CLOSE_STANDARD_STREAMS
                    && (inCloseable == System.out || inCloseable == System.err || inCloseable == System.in)) {
                return;
            }

            inCloseable.close();

        } catch (final Exception e) {
        }
    }

    public static URL getResourceUrl(String inResource) {

        return getResourceUrl(inResource, true);
    }

    public static URL getResourceUrl(String inResource, boolean inAllowUrlResolution) {

        if (inResource == null) {
            return null;
        }

        URL theIs = null;

        try {

            String theResource = inResource;
            theResource = theResource != null && theResource.length() > 0 && theResource.charAt(0) == '/'
                    ? theResource.substring(1)
                    : theResource;
            theIs = Thread.currentThread().getContextClassLoader().getResource(theResource);

        } catch (Exception e) {
            // swallow
        }

        try {
            theIs = theIs != null ? theIs : new File(inResource).toURI().toURL();
        } catch (Exception e) {
            // swallow
        }

        try {
            theIs = theIs != null || inAllowUrlResolution == false ? theIs : new URL(inResource);
        } catch (Exception e) {
            // swallow
        }

        return theIs;
    }

    public static <T> void forEach(T[] inArray, Consumer<T> inConsumer) {
        forEach(inArray, inConsumer);
    }

    public static <T> void forEach(T[] inArray, Consumer<T> inConsumer, Predicate<T> inPredicate) {
        if (inArray == null || inConsumer == null) {
            return;
        }
        Stream.of(inArray).filter(inPredicate == null ? inPredicate : i -> {
            return true;
        }).forEach(inConsumer);
    }

    public static void flush(Flushable inStream) {

        try {

            inStream.flush();

        } catch (final IOException e) {
            // swallow
        }
    }
}

Related

  1. load(String propsName)
  2. loadAttachApi()
  3. loadCorrectedConfiguration(String configurationFilename)
  4. loadFile(String file)
  5. loadGuiConfiguration(String configurationFilename)
  6. loadProperties(final String configFile, final String chainProperty)
  7. loadProperties(final String location)
  8. loadProperties(String propFile)
  9. loadPropertiesFromFile(File parent)