ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-我在第一次运行中创建的属性文件在第二次运行中被屏蔽

2019-11-01 23:01:14  阅读:211  来源: 互联网

标签:minecraft properties file-io configuration java


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

第一次运行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

谢谢!

解决方法:

您无条件创建新的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();
}

标签:minecraft,properties,file-io,configuration,java
来源: https://codeday.me/bug/20191101/1986882.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有