Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A properties file I created in the 1st run gets blanked in the 2nd run

Okay, I'm trying to create a custom client for Minecraft (don't worry, my question has nothing to do with Minecraft in particular), and I added an abstract class to manage a configuration file using Java's built-in Properties system. I have a method that loads a properties file or creates it if it doesn't already exist. This method is called at the beginning of all my other methods (although it only does anything the first time its called).

The properties file gets created just fine when I run Minecraft the first time, but somehow when I run it the second time, the file gets blanked out. I'm not sure where or why or how I'm wiping the file clean, can someone please help me? Here's my code; the offending method is 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");
    }
}

If it's too hard to read here, it's also on Pastebin: http://pastebin.com/9UscXWap

Thanks!

like image 514
Hayden Schiff Avatar asked Nov 23 '25 14:11

Hayden Schiff


1 Answers

You are unconditionally creating new FileOutputStream(cfile). This will overwrite the existing file with an empty one. You should only invoke the FileOutputStream constructor when writing a new config file.

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();
}
like image 149
erickson Avatar answered Nov 26 '25 05:11

erickson