Home > Software engineering >  Why doesn't Java create this file?
Why doesn't Java create this file?

Time:04-25

I'm working on a bigger project and just trying to create a file won't work. It is supposed to have a date and whatever the user puts in for an argument for a command in a Minecraft plugin.

My command class:


    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

        if (sender instanceof Player) {
            Player player = (Player) sender;

            if (args.length == 0) {
                player.sendMessage(ChatColor.RED   "You need to enter some arguments");
                player.sendMessage(ChatColor.YELLOW   "To save the server network to the GitHub website: /saveserver <what you built/did(TYPE _ FOR SPACES)>");
            } else if (args.length == 1) {
                File curdir = new File(System.getProperty("user.dir"));
                Path parentfolder = curdir.toPath().getParent().getParent();
                String networkfolder = parentfolder   File.separator   "Minecraft Servers";
                Date date = new Date();
                String filename = date   "-"   args[0].toUpperCase().replace("_", " ")   ".txt";

                File txt = new File(networkfolder   File.separator   filename);
                try {
                    txt.createNewFile();
                } catch (IOException e) {
                    player.sendMessage("Unable to create file.");
                }
            } else {
                player.sendMessage(ChatColor.RED   "Too many arguments");
                player.sendMessage(ChatColor.YELLOW   "To save the server network to the GitHub website: /saveserver <what you built/did(TYPE _ FOR SPACES)>");
            }
        }
        return true;
    }
}

It gives me this error:

[15:18:44 WARN]: java.io.IOException: The filename, directory name, or volume label syntax is incorrect
[15:18:44 WARN]:        at java.base/java.io.WinNTFileSystem.createFileExclusively(Native Method)
[15:18:44 WARN]:        at java.base/java.io.File.createNewFile(File.java:1043)
[15:18:44 WARN]:        at CrucialPlugin-1.0.jar//me.acclashcorporation.crucialplugin.commands.TestCrap.onCommand(TestCrap.java:36)
[15:18:44 WARN]:        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45)
[15:18:44 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159)
[15:18:44 WARN]:        at org.bukkit.craftbukkit.v1_18_R2.CraftServer.dispatchCommand(CraftServer.java:906)
[15:18:44 WARN]:        at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:2306)
[15:18:44 WARN]:        at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:2117)
[15:18:44 WARN]:        at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:2098)
[15:18:44 WARN]:        at net.minecraft.network.protocol.game.PacketPlayInChat.a(PacketPlayInChat.java:46)
[15:18:44 WARN]:        at net.minecraft.network.protocol.game.PacketPlayInChat.a(PacketPlayInChat.java:6)
[15:18:44 WARN]:        at net.minecraft.network.protocol.PlayerConnectionUtils.lambda$ensureRunningOnSameThread$1(PlayerConnectionUtils.java:51)
[15:18:44 WARN]:        at net.minecraft.server.TickTask.run(TickTask.java:18)
[15:18:44 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandler.d(IAsyncTaskHandler.java:153)
[15:18:44 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandlerReentrant.d(IAsyncTaskHandlerReentrant.java:24)
[15:18:44 WARN]:        at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:1399)
[15:18:44 WARN]:        at net.minecraft.server.MinecraftServer.d(MinecraftServer.java:188)
[15:18:44 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandler.y(IAsyncTaskHandler.java:126)
[15:18:44 WARN]:        at net.minecraft.server.MinecraftServer.be(MinecraftServer.java:1376)
[15:18:44 WARN]:        at net.minecraft.server.MinecraftServer.y(MinecraftServer.java:1369)
[15:18:44 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandler.c(IAsyncTaskHandler.java:136)
[15:18:44 WARN]:        at net.minecraft.server.MinecraftServer.x(MinecraftServer.java:1347)
[15:18:44 WARN]:        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:1229)
[15:18:44 WARN]:        at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:315)
[15:18:44 WARN]:        at java.base/java.lang.Thread.run(Thread.java:833)

CodePudding user response:

Like the error message says, the filename syntax is incorrect.

C:\Users\Anston Sorensen\Desktop\Minecraft Servers\Sun Apr 24 20:40:41 CDT 2022-D.txt

You can't use colons in Windows filenames, except for after the drive letter. You need to rethink your name generation strategy.

  • Related