Bukkit插件编程中.yml配置文件的创建和读取

 package com.sklm.config;

 import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map; public class PluginConfig { public static HashMap<String, Object> configMap = new HashMap<String,Object>();
public static String configPath = null;
public PluginConfig() {
try {
this.configPath = this.createFile();
this.readConfig();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 创建配置文件
* @return 返回创建文件的标准路径
* @throws IOException
*/
public String createFile() throws IOException {
boolean hasFile = true;
File plugins = null;
File messageConfig = null;
File messageBoard = null;
plugins = new File("./plugins");
if(!plugins.exists()) {
plugins.mkdirs();
}else {
messageBoard = new File(plugins.getCanonicalPath(),"messageBoard");
if(!messageBoard.exists()) {
messageBoard.mkdirs();
}else {
messageConfig = new File(messageBoard.getCanonicalPath(),"messageBoardConfig.yml");
if(!(hasFile=messageConfig.exists())) {
messageConfig.createNewFile();
}
}
}
if(!hasFile) {
//Bukkit.getLogger().info("BufferedOutputStream"+messageConfig.getCanonicalPath());
FileOutputStream fos = new FileOutputStream(messageConfig.getCanonicalPath().toString()); //
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(new String(" timerOut=10").getBytes());
if(bos != null) {
bos.close();
}
}
return messageConfig.getCanonicalPath().toString();
}
/**
* 读取配置文件中的信息
* @author SK_03
*
*/
public void readConfig() {
String path;
try {
int len = 0;
path = this.createFile();
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
while((len=br.read()) != -1) {
String configInfo = br.readLine();
if(configInfo != null && !(configInfo.contains("##"))) { //如果读取的内容不为空或者不是注释,则执行下面语句
//System.out.println(configInfo);
String[] configArray = configInfo.split("=");
//System.out.println(configArray[0]+configArray[1]);
configMap.put(configArray[0], configArray[1]);
//System.out.println(configArray[0].toString()+"="+configArray[1]);
}
}
if(br != null) {
br.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
上一篇:Qt5设置应用程序图标


下一篇:2017-11-28 中文编程语言之Z语言初尝试: ZLOGO 4