gpt4 book ai didi

java - 我在第一次运行时创建的属性文件在第二次运行时被清空

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:23:57 24 4
gpt4 key购买 nike

好的,我正在尝试为 Minecraft 创建自定义客户端(别担心,我的问题与 Minecraft 无关),我添加了一个抽象类来使用 Java 的内置管理配置文件属性系统。我有一个方法可以加载属性文件或创建它(如果它尚不存在)。此方法在我所有其他方法的开头被调用(尽管它只在第一次调用时执行任何操作)。

当我第一次运行 Minecraft 时,属性文件创建得很好,但不知何故,当我第二次运行它时,文件被清空了。我不确定在哪里、为什么或如何将文件擦干净,有人可以帮我吗?这是我的代码;有问题的方法是 loadConfig():

package net.minecraft.src;

import java.util.*;
import java.util.regex.*;
import java.io.*;

/**
* Class for managing my custom client's properties
*
* @author oxguy3
*/
public abstract class OxProps
{
public static boolean configloaded = false;
private static Properties props = new Properties();
private static String[] usernames;

public static void loadConfig() {
System.out.println("loadConfig() called");
if (!configloaded) {
System.out.println("loading config for the first time");
File cfile = new File("oxconfig.properties");
boolean configisnew;

if (!cfile.exists()) {
System.out.println("cfile failed exists(), creating blank file");
try {
configisnew = cfile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
configisnew=true;
}
} else {
System.out.println("cfile passed exists(), proceding");
configisnew=false;
}

FileInputStream cin = null;
FileOutputStream cout = null;
try {
cin = new FileInputStream(cfile);
cout = new FileOutputStream(cfile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

if (!configisnew) { //if the config already existed
System.out.println("config already existed");
try {
props.load(cin);
} catch (IOException e) {
e.printStackTrace();
}
} else { //if it doesn't exist, and therefore needs to be created
System.out.println("creating new config");
props.setProperty("names", "oxguy3, Player");
props.setProperty("cloak_url", "http://s3.amazonaws.com/MinecraftCloaks/akronman1.png");
try {
props.store(cout, "OXGUY3'S CUSTOM CLIENT\n\ncloak_url is the URL to get custom cloaks from\nnames are the usernames to give cloaks to\n");
cout.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
String names = props.getProperty("names");
System.out.println("names: "+names);
try {
usernames = Pattern.compile(", ").split(names);
} catch (NullPointerException npe) {
npe.printStackTrace();
}
System.out.println("usernames: "+Arrays.toString(usernames));
configloaded=true;
}
}

public static boolean checkUsername(String username) {
loadConfig();
System.out.println("Checking username...");
for (int i=0; i<usernames.length; i++) {
System.out.println("comparing "+username+" with config value "+usernames[i]);
if (username.startsWith(usernames[i])){
System.out.println("we got a match!");
return true;
}
}
System.out.println("no match found");
return false;
}

public static String getCloakUrl() {
loadConfig();
return props.getProperty("cloak_url", "http://s3.amazonaws.com/MinecraftCloaks/akronman1.png");
}
}

如果这里太难阅读,它也在 Pastebin 上:http://pastebin.com/9UscXWap

谢谢!

最佳答案

您正在无条件地创建 new FileOutputStream(cfile)。这将用一个空文件覆盖现有文件。您应该只在编写新配置文件时调用 FileOutputStream 构造函数。

if (configloaded) 
return;
File cfile = new File("oxconfig.properties");
try {
if (cfile.createNewFile()) {
try {
FileOutputStream cout = new FileOutputStream(cfile);
props.setProperty("names", "oxguy3, Player");
props.setProperty("cloak_url", "http://...");
...
cout.flush();
} finally {
cout.close();
}
} else {
FileInputStream cin = new FileInputStream(cfile);
try {
props.load(cin);
} finally {
cin.close();
}
}
configloaded=true;
} catch(IOException ex) {
e.printStackTrace();
}

关于java - 我在第一次运行时创建的属性文件在第二次运行时被清空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8672345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com