【问题标题】:Java Parent Directory Doesn't WorkJava 父目录不起作用
【发布时间】:2016-02-15 05:13:35
【问题描述】:

我正在尝试在保存玩家数据的特定文件夹中创建一个文件。但是,我遇到了两个问题。

  1. 文件未创建。
  2. 没有创建父文件。

任何想法都会很棒,我是文件处理的新手:S

public void saveData() {
    // SAVE THE DATA WHEN THE PLAYER QUITS!
   // if (!shouldCreateFile()) return;
    new BukkitRunnable() {
        @Override
        public void run() {
            System.out.println("DEBUG : " + f.getParentFile().exists());
            System.out.println("DEBUG : " + f.getParentFile());
            if (!f.exists()) {
                System.out.println("DEBUG : 2");
                try {
                    System.out.println("DEBUG : 3");
                    if(!f.getParentFile().exists()) f.getParentFile().mkdirs();
                    System.out.println("DEBUG : 4");
                    f.createNewFile();
                    System.out.println("DEBUG : 5");
                } catch (IOException e) {
                    Bukkit.getServer().getLogger().severe(ChatColor.RED + "******** Could not CREATE the PLAYERDATA file for " + uuid + "! ********");
                }
            }
            d.set("Settings", settings);
            d.set("Kills", kills);
            d.set("Duel_Wins", duelWins);
            d.set("Duel_Losses", duelLosses);
            d.set("Top_KS", topKillstreak);
            try {
                d.save(f);
            } catch (IOException e) {
                Bukkit.getServer().getLogger().severe(ChatColor.RED + "******** Could not SAVE the PLAYERDATA file for " + uuid + "! ********");
            }
        }
    }.runTaskAsynchronously(Main.getInstance());
}

输出到控制台的内容:

[21:10:36 INFO]: DEBUG : true
[21:10:36 INFO]: DEBUG : plugins/Main:playerdata

f是如何定义的:

    f = new File(Main.getInstance().getDataFolder() + File.pathSeparator + "playerdata", uuid.toString() + ".yml");
    d = YamlConfiguration.loadConfiguration(f);

再次感谢!

编辑: 看起来文件 WAS 已创建,但不在指定路径中。 Here is how it turned out.

【问题讨论】:

标签: java file configuration io config


【解决方案1】:

像这样声明你的 File 对象。

f = new File(Main.getInstance().getDataFolder() + File.pathSeparator + "playerdata", uuid.toString() + ".yml");

更多参考:

【讨论】:

    猜你喜欢
    • 2021-11-04
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多