mirror of
https://e.coding.net/circlecloud/CityBuild.git
synced 2024-11-22 10:58:50 +00:00
update FileConfig class...
Signed-off-by: j502647092 <jtb1@163.com>
This commit is contained in:
parent
56063ff2c2
commit
26fa367a06
@ -1,5 +1,6 @@
|
|||||||
package cn.citycraft.CityBuild.config;
|
package cn.citycraft.CityBuild.config;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
@ -7,6 +8,7 @@ import java.io.FileOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.io.Reader;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
@ -30,63 +32,79 @@ import com.google.common.io.Files;
|
|||||||
*/
|
*/
|
||||||
public class FileConfig extends YamlConfiguration {
|
public class FileConfig extends YamlConfiguration {
|
||||||
|
|
||||||
public static FileConfig init(File file) {
|
public static FileConfig init(File file) {
|
||||||
return FileConfig.loadConfiguration(file);
|
return FileConfig.loadConfiguration(file);
|
||||||
}
|
|
||||||
|
|
||||||
public static FileConfig loadConfiguration(File file) {
|
|
||||||
Validate.notNull(file, "File cannot be null");
|
|
||||||
FileConfig config = new FileConfig();
|
|
||||||
try {
|
|
||||||
config.load(file);
|
|
||||||
} catch (FileNotFoundException ex) {
|
|
||||||
} catch (IOException ex) {
|
|
||||||
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
|
|
||||||
} catch (InvalidConfigurationException ex) {
|
|
||||||
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
|
|
||||||
}
|
}
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected final DumperOptions yamlOptions = new DumperOptions();
|
public static FileConfig loadConfiguration(File file) {
|
||||||
|
Validate.notNull(file, "File cannot be null");
|
||||||
protected final Representer yamlRepresenter = new YamlRepresenter();
|
FileConfig config = new FileConfig();
|
||||||
|
try {
|
||||||
protected final Yaml yaml = new Yaml(new YamlConstructor(),
|
config.load(file);
|
||||||
yamlRepresenter, yamlOptions);
|
} catch (FileNotFoundException ex) {
|
||||||
|
} catch (IOException ex) {
|
||||||
@Override
|
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
|
||||||
public void load(File file) throws FileNotFoundException, IOException,
|
} catch (InvalidConfigurationException ex) {
|
||||||
InvalidConfigurationException {
|
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
|
||||||
Validate.notNull(file, "File cannot be null");
|
}
|
||||||
final FileInputStream stream = new FileInputStream(file);
|
return config;
|
||||||
load(new InputStreamReader(stream, Charsets.UTF_8));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void save(File file) throws IOException {
|
|
||||||
Validate.notNull(file, "File cannot be null");
|
|
||||||
Files.createParentDirs(file);
|
|
||||||
String data = saveToString();
|
|
||||||
Writer writer = new OutputStreamWriter(new FileOutputStream(file),
|
|
||||||
Charsets.UTF_8);
|
|
||||||
try {
|
|
||||||
writer.write(data);
|
|
||||||
} finally {
|
|
||||||
writer.close();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
protected final DumperOptions yamlOptions = new DumperOptions();
|
||||||
public String saveToString() {
|
|
||||||
yamlOptions.setIndent(options().indent());
|
protected final Representer yamlRepresenter = new YamlRepresenter();
|
||||||
yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
|
||||||
yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
protected final Yaml yaml = new Yaml(new YamlConstructor(),
|
||||||
String header = buildHeader();
|
yamlRepresenter, yamlOptions);
|
||||||
String dump = yaml.dump(getValues(false));
|
|
||||||
if (dump.equals(BLANK_CONFIG)) {
|
@Override
|
||||||
dump = "";
|
public void load(File file) throws FileNotFoundException, IOException,
|
||||||
|
InvalidConfigurationException {
|
||||||
|
Validate.notNull(file, "File cannot be null");
|
||||||
|
final FileInputStream stream = new FileInputStream(file);
|
||||||
|
load(new InputStreamReader(stream, Charsets.UTF_8));
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void load(Reader reader) throws IOException, InvalidConfigurationException {
|
||||||
|
BufferedReader input = (reader instanceof BufferedReader) ? (BufferedReader) reader
|
||||||
|
: new BufferedReader(reader);
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
try {
|
||||||
|
String line;
|
||||||
|
while ((line = input.readLine()) != null) {
|
||||||
|
builder.append(line);
|
||||||
|
builder.append('\n');
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
input.close();
|
||||||
|
}
|
||||||
|
loadFromString(builder.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(File file) throws IOException {
|
||||||
|
Validate.notNull(file, "File cannot be null");
|
||||||
|
Files.createParentDirs(file);
|
||||||
|
String data = saveToString();
|
||||||
|
Writer writer = new OutputStreamWriter(new FileOutputStream(file),
|
||||||
|
Charsets.UTF_8);
|
||||||
|
try {
|
||||||
|
writer.write(data);
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String saveToString() {
|
||||||
|
yamlOptions.setIndent(options().indent());
|
||||||
|
yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||||
|
yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||||
|
String header = buildHeader();
|
||||||
|
String dump = yaml.dump(getValues(false));
|
||||||
|
if (dump.equals(BLANK_CONFIG)) {
|
||||||
|
dump = "";
|
||||||
|
}
|
||||||
|
return header + dump;
|
||||||
}
|
}
|
||||||
return header + dump;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user