1 package com.sklm.config;
2
3 import java.io.BufferedOutputStream;
4 import java.io.BufferedReader;
5 import java.io.File;
6 import java.io.FileNotFoundException;
7 import java.io.FileOutputStream;
8 import java.io.FileReader;
9 import java.io.IOException;
10 import java.util.HashMap;
11 import java.util.Map;
12
13 public class PluginConfig {
14
15 public static HashMap<String, Object> configMap = new HashMap<String,Object>();
16 public static String configPath = null;
17 public PluginConfig() {
18 try {
19 this.configPath = this.createFile();
20 this.readConfig();
21 } catch (IOException e) {
22 // TODO Auto-generated catch block
23 e.printStackTrace();
24 }
25 }
26 /**
27 * 创建配置文件
28 * @return 返回创建文件的标准路径
29 * @throws IOException
30 */
31 public String createFile() throws IOException {
32 boolean hasFile = true;
33 File plugins = null;
34 File messageConfig = null;
35 File messageBoard = null;
36 plugins = new File("./plugins");
37 if(!plugins.exists()) {
38 plugins.mkdirs();
39 }else {
40 messageBoard = new File(plugins.getCanonicalPath(),"messageBoard");
41 if(!messageBoard.exists()) {
42 messageBoard.mkdirs();
43 }else {
44 messageConfig = new File(messageBoard.getCanonicalPath(),"messageBoardConfig.yml");
45 if(!(hasFile=messageConfig.exists())) {
46 messageConfig.createNewFile();
47 }
48 }
49 }
50 if(!hasFile) {
51 //Bukkit.getLogger().info("BufferedOutputStream"+messageConfig.getCanonicalPath());
52 FileOutputStream fos = new FileOutputStream(messageConfig.getCanonicalPath().toString()); //
53 BufferedOutputStream bos = new BufferedOutputStream(fos);
54 bos.write(new String(" timerOut=10").getBytes());
55 if(bos != null) {
56 bos.close();
57 }
58 }
59 return messageConfig.getCanonicalPath().toString();
60 }
61 /**
62 * 读取配置文件中的信息
63 * @author SK_03
64 *
65 */
66 public void readConfig() {
67 String path;
68 try {
69 int len = 0;
70 path = this.createFile();
71 FileReader fr = new FileReader(path);
72 BufferedReader br = new BufferedReader(fr);
73 while((len=br.read()) != -1) {
74 String configInfo = br.readLine();
75 if(configInfo != null && !(configInfo.contains("##"))) { //如果读取的内容不为空或者不是注释,则执行下面语句
76 //System.out.println(configInfo);
77 String[] configArray = configInfo.split("=");
78 //System.out.println(configArray[0]+configArray[1]);
79 configMap.put(configArray[0], configArray[1]);
80 //System.out.println(configArray[0].toString()+"="+configArray[1]);
81 }
82 }
83 if(br != null) {
84 br.close();
85 }
86 } catch (FileNotFoundException e) {
87 // TODO Auto-generated catch block
88 e.printStackTrace();
89 }catch (IOException e) {
90 // TODO Auto-generated catch block
91 e.printStackTrace();
92 }
93 }
94
95
96
97 }