瀏覽代碼

Improve file handling.

GJ 13 年之前
父節點
當前提交
4aecb9756e
共有 1 個文件被更改,包括 27 次插入7 次删除
  1. 27 7
      src/main/java/com/gmail/nossr50/commands/mc/McremoveCommand.java

+ 27 - 7
src/main/java/com/gmail/nossr50/commands/mc/McremoveCommand.java

@@ -3,6 +3,7 @@ package com.gmail.nossr50.commands.mc;
 import java.io.BufferedReader;
 import java.io.FileReader;
 import java.io.FileWriter;
+import java.io.IOException;
 
 import org.bukkit.ChatColor;
 import org.bukkit.OfflinePlayer;
@@ -118,9 +119,12 @@ public class McremoveCommand implements CommandExecutor {
     private boolean removeFlatFileUser(String playerName) {
         boolean worked = false;
 
+        BufferedReader in = null;
+        FileWriter out = null;
+
         try {
             FileReader file = new FileReader(location);
-            BufferedReader in = new BufferedReader(file);
+            in = new BufferedReader(file);
             StringBuilder writer = new StringBuilder();
             String line = "";
 
@@ -137,16 +141,32 @@ public class McremoveCommand implements CommandExecutor {
                 }
             }
 
-            in.close();
-            FileWriter out = new FileWriter(location); //Write out the new file
+            out = new FileWriter(location); //Write out the new file
             out.write(writer.toString());
-            out.close();
-
-            return worked;
         }
         catch (Exception e) {
             plugin.getLogger().severe("Exception while reading " + location + " (Are you sure you formatted it correctly?)" + e.toString());
-            return worked;
         }
+        finally {
+            if (in != null) {
+                try {
+                    in.close();
+                }
+                catch (IOException ex) {
+                    ex.printStackTrace();
+                }
+            }
+
+            if (out != null) {
+                try {
+                    out.close();
+                }
+                catch (IOException ex) {
+                    ex.printStackTrace();
+                }
+            }
+        }
+
+        return worked;
     }
 }