PR 36084: Temp. files were not being deleted because open file streams were not being closed properly.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@232415 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brent Worden 2005-08-13 04:06:37 +00:00
parent 4eb8044ae7
commit 47b45c6fad
1 changed files with 27 additions and 8 deletions

View File

@ -19,6 +19,7 @@ package org.apache.commons.math;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
@ -74,25 +75,43 @@ public class TestUtils {
Object result = null;
File tmp = null;
FileOutputStream fo = null;
FileInputStream fi = null;
try {
// serialize the Object
tmp = File.createTempFile("test",".ser");
FileOutputStream fo = new FileOutputStream(tmp);
fo = new FileOutputStream(tmp);
ObjectOutputStream so = new ObjectOutputStream(fo);
so.writeObject(o);
so.flush();
fo.close();
// deserialize the Book
FileInputStream fi = new FileInputStream(tmp);
fi = new FileInputStream(tmp);
ObjectInputStream si = new ObjectInputStream(fi);
result = si.readObject();
} catch (Exception ex) {
}catch (Exception e) {
e.printStackTrace();
}finally{
if(tmp != null) tmp.delete();
} finally {
if (fo != null) {
try {
fo.close();
} catch (IOException ex) {
}
}
if (fi != null) {
try {
fi.close();
} catch (IOException ex) {
}
}
}
if (tmp != null) {
tmp.delete();
}
return result;