Java 我在第一次运行中创建的属性文件在第二次运行中被清除

Java 我在第一次运行中创建的属性文件在第二次运行中被清除,java,configuration,file-io,properties,minecraft,Java,Configuration,File Io,Properties,Minecraft,好的,我正在尝试为Minecraft创建一个自定义客户端(别担心,我的问题与Minecraft没有特别的关系),我添加了一个抽象类来使用Java的内置属性系统管理配置文件。我有一个方法,可以加载属性文件,或者在它不存在时创建它。这个方法在我所有其他方法的开头被调用(尽管它只在第一次被调用时做任何事情) 当我第一次运行Minecraft时,属性文件创建得很好,但是当我第二次运行它时,该文件被屏蔽了。我不知道我在哪里,为什么或者怎样把文件擦干净,有人能帮我吗?这是我的密码;有问题的方法是loadCo

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

当我第一次运行Minecraft时,属性文件创建得很好,但是当我第二次运行它时,该文件被屏蔽了。我不知道我在哪里,为什么或者怎样把文件擦干净,有人能帮我吗?这是我的密码;有问题的方法是loadConfig():

package net.minecraft.src;
导入java.util.*;
导入java.util.regex.*;
导入java.io.*;
/**
*用于管理自定义客户端属性的类
*
*@作者oxguy3
*/
公共抽象类恶唑
{
公共静态布尔configloaded=false;
私有静态属性props=新属性();
私有静态字符串[]用户名;
公共静态void loadConfig(){
System.out.println(“调用了loadConfig());
如果(!configloaded){
System.out.println(“首次加载配置”);
File cfile=新文件(“oxconfig.properties”);
布尔配置是新的;
如果(!cfile.exists()){
System.out.println(“cfile failed exists(),正在创建空白文件”);
试一试{
configisnew=cfile.createNewFile();
}捕获(IOE异常){
e、 printStackTrace();
configisnew=true;
}
}否则{
System.out.println(“cfile passed exists(),proceding”);
configisnew=false;
}
FileInputStream cin=null;
FileOutputStream cout=null;
试一试{
cin=新文件输入流(cfile);
cout=新文件输出流(cfile);
}catch(filenotfounde异常){
e、 printStackTrace();
}
如果(!configisnew){//如果配置已经存在
System.out.println(“配置已存在”);
试一试{
道具载荷(cin);
}捕获(IOE异常){
e、 printStackTrace();
}
}else{//如果它不存在,因此需要创建它
System.out.println(“创建新配置”);
道具.setProperty(“名称”、“oxguy3、玩家”);
props.setProperty(“斗篷url”http://s3.amazonaws.com/MinecraftCloaks/akronman1.png");
试一试{
props.store(cout,“OXGUY3的自定义客户端\n\nClak_url是从中获取自定义斗篷的url \n名称是向其提供斗篷的用户名”);
cout.flush();
}捕获(IOE异常){
e、 printStackTrace();
}
}
字符串名称=props.getProperty(“名称”);
System.out.println(“名称:”+名称);
试一试{
usernames=Pattern.compile(“,”).split(名称);
}捕获(NullPointerException npe){
npe.printStackTrace();
}
System.out.println(“用户名:”+Arrays.toString(用户名));
configloaded=true;
}
}
公共静态布尔校验用户名(字符串用户名){
loadConfig();
System.out.println(“检查用户名…”);

对于(int i=0;i您正在无条件地创建
新文件输出流(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();
}

编辑此站点的代码;不能保证Pastebin将与StackOverflow同时启动。@Makoto好的,完成-我现在将其发布在这里。我将该构造函数移动到
if(!cfile.exists())
语句中,它工作得很好。谢谢erickson!
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();
}