Better file handling

This commit is contained in:
GJ 2012-07-04 10:00:11 -04:00
parent dbc7277ba9
commit 124aa72fd9

View File

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