2016-07-30 101 views
0

我想在Java中创建一个属性文件。可悲的是,当我启动Minecraft时(因为这是Forge中的一个mod),该文件不会被创建。我会非常感谢任何帮助我的人。以下是代码:为什么不创建属性文件?

package mymod.properties; 

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.Properties; 

public class WriteToProperties { 

public static void main(String[] args) { 

    Properties prop = new Properties(); 
    try { 
     prop.setProperty("Test", "Yay"); 

     prop.store(new FileOutputStream("Test.properties"), null); 

    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 

} 

} 
+1

是否有可能被在一个目录你不创建该文件的所有喜好没有期望?尝试指定完整路径。 – JustinKSU

回答

-1

此文件是在您的项目的根目录下创建的。如果你想一些具体的保存路径,在您的项目像道具的根目录中创建文件夹,并在FileOutputStream构造改变"props\\Test.properties"

+0

好的。项目的根源究竟是什么?有没有办法检查? (或者它只是第一个包含所有内容的文件?) – Betterjakers

+0

你用什么IDE编写这段代码? Eclipse,IDEA还是只有记事本? –

+0

我使用Eclipse。 :) – Betterjakers

1

基本上你想要的是initialize(event.getSuggestedConfigurationFile());

伪造基本上希望交给你的默认路径设置给你。我也建议你在逻辑上构建一些东西,所以它很好,干净并且稍后可以访问,并且更易于管理。

我用这个作为一个配置装载机

package tschallacka.magiccookies.init; 

import java.io.File; 

import net.minecraftforge.common.config.Configuration; 
import net.minecraftforge.common.config.Property; 
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; 
import tschallacka.magiccookies.MagicCookies; 
import tschallacka.magiccookies.api.ModHooks; 
import tschallacka.magiccookies.api.Preferences; 

public class ConfigLoader { 
    public static Configuration config; 
    public static final String CATEGORY_GENERAL = "GeneralSettings"; 
    public static final String CATEGORY_SERVER = "ServerPerformance"; 
    public static void init(FMLPreInitializationEvent event) { 
     ModHooks.MODID = MagicCookies.MODID; 
     ModHooks.MODNAME = MagicCookies.MODNAME; 
     ModHooks.VERSION = MagicCookies.VERSION; 
     try { 
      initialize(event.getSuggestedConfigurationFile()); 
     } 
     catch (Exception e) { 
      MagicCookies.log.error("MagicCookie failed to load preferences. Reverting to default"); 
     } 
     finally { 
      if (config != null) { 
       save(); 
      } 
     } 
    } 
    private static void initialize(final File file) { 
     config = new Configuration(file); 
     config.addCustomCategoryComment(CATEGORY_GENERAL, "General Settings"); 
     config.addCustomCategoryComment(CATEGORY_SERVER, "Server performance settings"); 
     Preferences.magicCookieIsLoaded = true; 
     config.load(); 
     syncConfigurable(); 
    } 
    private static void save() { 
     config.save(); 
    } 
    private static void syncConfigurable() { 

     final Property awesomeMod = config.get(Configuration.CATEGORY_GENERAL, "awesome_mod", Preferences.awesomeMod); 
     awesomeMod.comment = "Set this to yes if you think this mod is awesome, maybe it will at some time unlock a special thing.... or not... but that's only for people who think this is an wesome Mod ;-)"; 
     Preferences.awesomeMod = awesomeMod.getString(); 

     final Property numberOfBlocksPlacingPerTickByStripper = config.get(CATEGORY_SERVER,"number_of_blocks_placing_per_tick_by_stripper",Preferences.numberOfBlocksPlacingPerTickByStripper); 
     numberOfBlocksPlacingPerTickByStripper.comment = "This affects how many blocks will be placed per tick by the creative stripper tool per tick. The stripper tool will only place blocks if ticks are take less than 50ms. If you experience lag lower this number, if you don't experience lag and want faster copy pasting, make this number higher. For an awesome slowmo build of your caste set this to 1 ;-). Set to 0 to render everything in one go per chunk"; 
     Preferences.numberOfBlocksPlacingPerTickByStripper = numberOfBlocksPlacingPerTickByStripper.getInt(); 

     final Property averageTickTimeCalculationSpan = config.get(CATEGORY_SERVER,"number_of_ticks_used_for_average_time_per_tick_calculation",Preferences.averageTickTimeCalculationSpan); 
     averageTickTimeCalculationSpan.comment = "This number is the number of tick execution times are added together to calculation an average. The higher number means less lag by some things like the strippers, but can also lead to longer execution times for the strippers. Basically if your server is always running behind on server ticks set this value to 1, to at least get some work done when your server is running under 50ms tickspeed"; 
     Preferences.averageTickTimeCalculationSpan = averageTickTimeCalculationSpan.getInt(); 

     final Property acceptableTickduration = config.get(CATEGORY_SERVER,"acceptable_tick_duration",Preferences.acceptableTickduration); 
     acceptableTickduration.comment = "Define here what you see as acceptable tick speed where MagicCookies can do some of its magic. If average tick speed or current tick speed is higher than this value it won't perform some tasks to help manage server load."; 
     Preferences.acceptableTickduration = (long)acceptableTickduration.getDouble(); 
    } 
} 

的值保持Preferences类。 这完全是这样,我可以在任何地方做Preferences.valuename

package tschallacka.magiccookies.api; 
/** 
* Here the preferences of MagicCookies will be stored 
* and kept after the config's are loaded. 
* @author Tschallacka 
* 
*/ 
public class Preferences { 
    public static boolean magicCookieIsLoaded = false; 
    public static String awesomeMod = "Well?"; 
    public static boolean isLoadedThaumcraft = false; 
    public static int numberOfBlocksPlacingPerTickByStripper = 0; 
    public static int averageTickTimeCalculationSpan = 60; 
    public static long acceptableTickduration = 50l; 
    public static int darkShrineFrequency = 0; 
    public static boolean darkShrineSpawnLogging = true; 
    public static boolean entropyTempleSpawnLogging = true; 
    public static int entropySize = 30; 
} 

在你的主MOD文件:

@EventHandler 
public void preInit(FMLPreInitializationEvent e) { 
    MagicCookies.log.warn("Preinit starting"); 
    MinecraftForge.EVENT_BUS.register((Object)MagicCookies.instance); 
    ConfigLoader.init(e); 
} 

之后,你可以只取出从Preferences类

相关问题