Solved FileInputStream Error

Discussion in 'Spigot Plugin Development' started by DatSquidFTW, Jan 28, 2020.

  1. So I'm making a custom currency plugin and I have this error with the input stream. I'm getting an EOFException but I think I'm writing to the input stream just fine. Maybe I'm just tired. But does anyone have an idea of what this might be? Below are the code and the error from the console.
    Code (Java):
    public void loadTrophyFile() throws IOException, ClassNotFoundException {
            File file = new File(Bukkit.getPluginManager().getPlugin("Trophies").getDataFolder(), "currency.dat");
            if (!file.exists()){
                boolean success = file.createNewFile();
                System.out.println("[Trophies] Currency.dat file creation: " + success);
            }
            ObjectInputStream input = new ObjectInputStream(new FileInputStream(file));
            Object readObject = input.readObject();
            input.close();

            if (!(readObject instanceof HashMap)){
                throw new IOException("Data is not in a hashmap");
            }
            mobCoins = (HashMap<UUID, Integer>) readObject;
            for (UUID key : mobCoins.keySet()){
                mobCoins.put(key, mobCoins.get(key));
            }
        }
    Code (YAML):
     java.io.EOFException
    [20:37:47 WARN]
    :       at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
    [20:37:47 WARN]
    :       at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
    [20:37:47 WARN]
    :       at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    [20:37:47 WARN]
    :       at java.io.ObjectInputStream.<init>(Unknown Source)
    [20:37:47 WARN]
    :       at com.champmc.trophies.managers.TrophyManager.loadTrophyFile(TrophyManager.java:51)
    [20:37:47 WARN]
    :       at com.champmc.trophies.Trophies.registerManagers(Trophies.java:41)
    [20:37:47 WARN]
    :       at com.champmc.trophies.Trophies.onEnable(Trophies.java:18)
    [20:37:47 WARN]
    :       at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321)
    [20:37:47 WARN]
    :       at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340)
    [20:37:47 WARN]
    :       at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405)
    [20:37:47 WARN]
    :       at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357)
    [20:37:47 WARN]
    :       at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317)
    [20:37:47 WARN]
    :       at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:414)
    [20:37:47 WARN]
    :       at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:378)
    [20:37:47 WARN]
    :       at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:333)
    [20:37:47 WARN]
    :       at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:263)
    [20:37:47 WARN]
    :       at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525)
    [20:37:47 WARN]
    :       at java.lang.Thread.run(Unknown Source)
    Line 51 is the one with the ObjectInputStream definition
     
  2. ObjectInputStream will always throw an exception when it reaches the end of the file (That's why it's called EOFException "End of File Exception"). You just need to try/catch it, then close the reader, and continue your code.
     
  3. Sounds good. Thank you!