get Properties File - Java java.util

Java examples for java.util:Properties File

Description

get Properties File

Demo Code


import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.Properties;

public class Main{
    public static void main(String[] argv) throws Exception{
        Class classType = String.class;
        String fileName = "java2s.com";
        System.out.println(getPropertiesFile(classType,fileName));
    }//from  ww  w.  java2 s .co m
    public static Properties getPropertiesFile(Class<?> classType,
            String fileName) throws AppException {
        URL fileURL = getURLFile(classType, fileName);

        InputStream in = null;
        try {
            in = fileURL.openStream();
            Reader reader = new InputStreamReader(in, "UTF-8");

            Properties properties = new Properties();

            try {
                properties.load(reader);
            } finally {
                reader.close();
            }

            return properties;
        } catch (IOException e) {
            throw new AppException(e.getCause());
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    public static URL getURLFile(Class<?> classType, String fileName) {
        URL fileURL = classType.getClassLoader().getResource(fileName);
        return fileURL;
    }
}

Related Tutorials