Ver Fonte

Better file handling

GJ há 13 anos atrás
pai
commit
124aa72fd9
1 ficheiros alterados com 25 adições e 7 exclusões
  1. 25 7
      src/main/java/com/gmail/nossr50/spout/SpoutStuff.java

+ 25 - 7
src/main/java/com/gmail/nossr50/spout/SpoutStuff.java

@@ -46,26 +46,24 @@ public class SpoutStuff {
      * @param theFilePath The name of the file path
      */
     private static void writeFile(String theFileName, String theFilePath) {
+        OutputStream os = null;
+        JarFile jar = null;
+
         try {
             File currentFile = new File(theFilePath + theFileName);
 
-            JarFile jar = new JarFile(mcMMO.mcmmo);
+            jar = new JarFile(mcMMO.mcmmo);
             JarEntry entry = jar.getJarEntry("resources/" + theFileName);
             InputStream is = jar.getInputStream(entry);
 
-
             byte[] buf = new byte[2048];
             int nbRead;
 
-            OutputStream os = new BufferedOutputStream(new FileOutputStream(currentFile));
+            os = new BufferedOutputStream(new FileOutputStream(currentFile));
 
             while ((nbRead = is.read(buf)) != -1) {
                 os.write(buf, 0, nbRead);
             }
-
-            os.flush();
-            os.close();
-            jar.close();
         }
         catch (FileNotFoundException e) {
             e.printStackTrace();
@@ -73,6 +71,26 @@ public class SpoutStuff {
         catch (IOException e) {
             e.printStackTrace();
         }
+        finally {
+            if (os != null) {
+                try {
+                    os.flush();
+                    os.close();
+                }
+                catch (IOException ex) {
+                    ex.printStackTrace();
+                }
+            }
+
+            if (jar != null) {
+                try {
+                    jar.close();
+                }
+                catch (IOException ex) {
+                    ex.printStackTrace();
+                }
+            }
+        }
     }
 
     /**