[웹][자바] 간단한 설정 파일 / 간단한 값 파일에서 불러오기

setting 파일 만들기 / 자바에서 .ini 만들기


// config.properties
#Fri Jan 17 22:37:45 MYT 2014
domain-1=www.a.net,www.afjkdlaj-fdsf.vjlds
domain-2=www.b.net,www.bfjkdlaj-fdsf.vjlds
domain-3=www.c.net,
domain-4=www.d.net


// Main.java
public class Main {

    private static String DOMAIN_MAPPING_DELIMITER = ",";

    public static void main(String[] args) {


        Properties prop = new Properties();
        InputStream input = null;

        try {

            String basePath = "d:\\mine\\programming\\gwt\\plainJava\\src";

            final String CONFIG_DOMAIN_MAPPING_FOR_LOG_FILE_NAME = "config.properties";
            Path configPath = getConfigPath(basePath, CONFIG_DOMAIN_MAPPING_FOR_LOG_FILE_NAME);
            if(configPath == null){
                return;
            }

            input = new FileInputStream(configPath.toString());

            // load a properties file
            prop.load(input);


            HashMap<String, String> domainMap = new HashMap<String, String>();

            String[] keyValue = null;
            String propValue = null;
            String value = null;
            String pname = null;

            for (Enumeration<?> e = prop.propertyNames(); e.hasMoreElements();) {
                pname = e.nextElement().toString();
                propValue = prop.getProperty(pname);

                // Get key & value
                keyValue = propValue.split(DOMAIN_MAPPING_DELIMITER);

                // Add to the map
                if (keyValue.length > 1)
                    value = keyValue[1];
                else if (keyValue.length == 1)
                    value = "";
                domainMap.put(keyValue[0], value);
            }



            String domain = "www.a.net";
            String mappedDomain = domainMap.get(domain);
            System.out.println(mappedDomain);


        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }



    }

    private static Path getConfigPath(String basePath, String fileName) {
        Path configPath = Paths.get(basePath).resolve(fileName);
        try {
            Path fp = configPath.toRealPath();
            return fp;
        } catch (NoSuchFileException x) {
            // file doesn't exist.

        } catch (IOException x) {
            // Logic for other sort of file error.
        }

        return null;

    }
}




댓글 없음:

댓글 쓰기