当前位置: 首页 > 知识库问答 >
问题:

为什么不创建属性文件?

房泉
2023-03-14

我试图使一个属性文件在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();
    }

}

}

共有2个答案

昝成弘
2023-03-14

此文件是在项目的根目录中创建的。如果要保存某些特定路径,请在项目的根目录中创建类似于props的文件夹,并将FileOutputStream构造函数中的路径更改为“props\\Test.properties”

司徒钱青
2023-03-14

基本上,您需要的是初始化(event.getSuggestedConfigurationFile())

Forge基本上希望把默认设置交给你。我还建议你逻辑地组织事情,这样它就很好,干净,以后可以访问,而且更容易管理。

我使用它作为配置加载程序

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();
    }
}

值持有者首选项类。这纯粹是为了我能做首选项。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);
}

之后,您可以从首选项类中获取所有首选项

 类似资料:
  • 问题内容: 也许这是一个明显的答案,但是 为什么 地球上 会浏览器决定创建自己的供应商前缀之类的? 我的意思是:为什么我必须输入: 是否因为每个平台都认为“我们很酷,我们会想出一种 更好的 方法来制作圆角”?键入三行似乎是完全和莫名其妙的冗余。 问题答案: 这是因为这些功能是在规范到达最终发布阶段之前由供应商实施的。 供应商前缀确保与功能更改等没有冲突。 最初,供应商前缀的目的是允许浏览器制造商开

  • 问题内容: SQLAlchemy正在生成但未启用Postgresql中列的序列。我怀疑我在引擎设置中可能做错了什么。 使用SQLAlchemy教程(http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html)中的示例: 使用此脚本,将生成下表: 然而,序列 被 创建: SQLAlchemy 0.9.1,Python 2.7.5 +,Postgre

  • OrientDB中的属性就像数据库表中的类和列的字段。 创建属性是用于为特定类创建属性的命令。 在命令中使用的类名必须存在。 以下语句是创建属性命令的基本语法。 以下是有关上述语法中选项的详细信息。 - 定义要在其中创建属性的类。 - 定义属性的逻辑名称。 - 定义您想要创建的属性的类型。 - 定义容器属性类型中使用的容器类型。 - 定义容器属性类型中使用的容器类。 下表提供了属性的数据类型,以便

  • 问题内容: 我了解JSON,但不了解JSONP。Wikipedia上有关JSON的文档是JSONP的最高搜索结果。它说: JSONP或“带填充的JSON”是JSON扩展,其中将前缀指定为调用本身的输入参数。 ??什么电话 这对我来说毫无意义。JSON是一种数据格式。没有电话 在第二个搜索结果是由某些人叫雷米,谁写的这个约JSONP: JSONP是脚本标记注入,它将响应从服务器传递到用户指定的函数。

  • 问题内容: 我有以下hibernate.cfg.xml: 我尝试了另一种方言(),但结果却很旧 pom.xml: 当调用以下代码行时: 我看到以下堆栈跟踪: 这个问题的原因是什么? 如何解决? 聚苯乙烯 数据库架构在MySql中不存在! 如果我明确添加数据库shema-一切正常。 从Java应用程序创建架构的方法在哪里? 问题答案: 我通常在使用Spring时使用属性文件自动创建数据库,以下是它的

  • 每一个国家的都有其特殊国情,主要是原因是中国的网络太慢,及中国存在大量老旧的计算机,它们预装着window XP,IE浏览器最高只能升级到IE8, 出于这两方面的原因,我们需要一个体积更少,兼容性更好的React。并且之前facebook也闹过LICENSE问题,更是促进中国互联网公司决定自主研发框架,努力摆脱对外国框架的依赖。 对于我们公司而言,一个旅游公司, 在线上订火车票,飞机票, 景点门票