Skip to content

Commit

Permalink
Close streams even if catch occurs (avoid memory leaks)
Browse files Browse the repository at this point in the history
  • Loading branch information
bean5 committed Jul 16, 2021
1 parent d397ec2 commit 574a2b5
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions cs108library4a/src/main/java/com/csl/cs108library4a/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,20 @@ void debugFileClose() {
}
void debugFileEnable(boolean enable) { enableFileDebug = enable; }
void writeDebug2File(String stringDebug) {
if (fileDebug != null && enableFileDebug) {
if (fileDebug != null && inventoring) {
FileOutputStream outputStreamDebug;
PrintWriter printWriterDebug;
try {
FileOutputStream outputStreamDebug = new FileOutputStream(fileDebug, true);
PrintWriter printWriterDebug = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(outputStreamDebug), "UTF-8"));
outputStreamDebug = new FileOutputStream(fileDebug, true);
printWriterDebug = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(outputStreamDebug), "UTF-8"));
printWriterDebug.println(stringDebug);
printWriterDebug.flush(); printWriterDebug.close();
outputStreamDebug.close();
} catch (Exception ex) {
}
if(outputStreamDebug != null) outputStreamDebug.close();
if(printWriterDebug) {
printWriterDebug.flush();
printWriterDebug.close();
}
}
}
}

0 comments on commit 574a2b5

Please sign in to comment.