为什么 Java 不创建这个文件?
Why doesn't Java create this file?
我正在处理一个更大的项目,只是尝试创建一个文件是行不通的。它应该有一个日期以及用户在 Minecraft 插件中为命令的参数输入的任何内容。
我的命令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;
}
}
它给我这个错误:
[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(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[=12=](MinecraftServer.java:315)
[15:18:44 WARN]: at java.base/java.lang.Thread.run(Thread.java:833)
如错误消息所述,文件名语法不正确。
C:\Users\Anston Sorensen\Desktop\Minecraft Servers\Sun Apr 24 20:40:41 CDT 2022-D.txt
您不能在 Windows 文件名中使用冒号,驱动器号后除外。您需要重新考虑您的名称生成策略。
我正在处理一个更大的项目,只是尝试创建一个文件是行不通的。它应该有一个日期以及用户在 Minecraft 插件中为命令的参数输入的任何内容。
我的命令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;
}
}
它给我这个错误:
[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(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[=12=](MinecraftServer.java:315)
[15:18:44 WARN]: at java.base/java.lang.Thread.run(Thread.java:833)
如错误消息所述,文件名语法不正确。
C:\Users\Anston Sorensen\Desktop\Minecraft Servers\Sun Apr 24 20:40:41 CDT 2022-D.txt
您不能在 Windows 文件名中使用冒号,驱动器号后除外。您需要重新考虑您的名称生成策略。