package com.saki.plugin.dictionary;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import com.saki.tools.FileUtil;

public class ConfigUtils {
    public static String readConfig(String path, String key, String defaultValue) {
        Properties p = new Properties();
        try {
            FileInputStream in;
            p.load(in = new FileInputStream(new File(FileUtil.BASE_PATH + path)));
            in.close();
            return p.getProperty(key, defaultValue);
        } catch (IOException e) {
            // e.printStackTrace();
        }
        return defaultValue;
    }

    public static void writeConfig(String path, String key, String value) {
        Properties p = new Properties();
        try {
            File f = new File(FileUtil.BASE_PATH + path);
            if (!f.exists()) {
                f.getParentFile().mkdirs();
                f.createNewFile();
            }
            FileInputStream in;
            p.load(in = new FileInputStream(f));
            in.close();
            p.put(key, value);
            FileOutputStream out;
            p.store(out = new FileOutputStream(f), null);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
