Last active
August 29, 2015 14:22
-
-
Save nesheep5/ce708c9375a7bb05370e to your computer and use it in GitHub Desktop.
try-with-resourcesでリソース解放されないパターン ref: http://qiita.com/nesheep5/items/6a68d862c5902e5994a4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| File file = new File("out.txt"); | |
| // PrintWriterがインスタンス生成に失敗すると、BufferedWriter・FileWriterが解放されない | |
| try(PrintWriter pw = | |
| new PrintWriter(new BufferedWriter(new FileWriter(file)));) { | |
| // 処理 | |
| } | |
| // ・・・ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ・・・ | |
| // 個別にフィールドを宣言し、それぞれインスタンス生成する | |
| try ( FileWriter fw = new FileWriterWrapper(file); | |
| BufferedWriter bw = new BufferedWriterWrapper(fw); | |
| PrintWriter pw = new PrintWriterWrapper(bw);) { | |
| System.out.println("func"); | |
| } | |
| // ・・・ | |
| // PrintWriterWrapperのコンストラクタで例外発生させる | |
| public PrintWriterWrapper(Writer out) { | |
| super(out); | |
| System.out.println("ERROR!! new PrintWriter"); | |
| thorow new RuntimeException(); | |
| } | |
| // ・・・ | |
| // 実行結果(close()が実行されている) | |
| // new FileWriter | |
| // new BufferedWriter | |
| // ERROR!! new PrintWriter | |
| // close BufferedWriter | |
| // close FileWriter | |
| // close FileWriter | |
| // catch:java.lang.RuntimeException | |
| // finally | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ・・・ | |
| // ネストでインスタンス生成する | |
| try (PrintWriter pw = | |
| new PrintWriterWrapper( | |
| new BufferedWriterWrapper( | |
| new FileWriterWrapper(file)));) { | |
| System.out.println("func"); | |
| } | |
| // ・・・ | |
| // 実行結果(close()が実行されている) | |
| // new FileWriter | |
| // new BufferedWriter | |
| // new PrintWriter | |
| // func | |
| // close PrintWriter | |
| // close BufferedWriter | |
| // close FileWriter | |
| // finally |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ・・・ | |
| // ネストでインスタンス生成する | |
| try (PrintWriter pw = | |
| new PrintWriterWrapper( | |
| new BufferedWriterWrapper( | |
| new FileWriterWrapper(file)));) { | |
| System.out.println("func"); | |
| } | |
| // ・・・ | |
| // PrintWriterWrapperのコンストラクタで例外発生させる | |
| public PrintWriterWrapper(Writer out) { | |
| super(out); | |
| System.out.println("ERROR!! new PrintWriter"); | |
| thorow new RuntimeException(); | |
| } // ・・・ | |
| // 実行結果(close()が実行されない) | |
| // new FileWriter | |
| // new BufferedWriter | |
| // ERROR!! new PrintWriter | |
| // catch:java.lang.RuntimeException | |
| // finally | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Test { | |
| public static void main(String[] args) { | |
| File file = new File("out.txt"); | |
| try (PrintWriter pw = | |
| new PrintWriterWrapper( | |
| new BufferedWriterWrapper( | |
| new FileWriterWrapper(file)));) { | |
| System.out.println("func"); | |
| } catch (Exception e) { | |
| System.out.println("catch:" + e); | |
| } finally { | |
| System.out.println("finally"); | |
| } | |
| } | |
| // 以下、ログを追加したラッパークラス | |
| public static class PrintWriterWrapper extends PrintWriter { | |
| public PrintWriterWrapper(Writer out) { | |
| super(out); | |
| System.out.println("new PrintWriter"); | |
| } | |
| @Override | |
| public void close() { | |
| System.out.println("close PrintWriter"); | |
| super.close(); | |
| } | |
| } | |
| public static class BufferedWriterWrapper extends BufferedWriter { | |
| public BufferedWriterWrapper(Writer out) { | |
| super(out); | |
| System.out.println("new BufferedWriter"); | |
| throw new RuntimeException(); | |
| } | |
| @Override | |
| public void close() throws IOException { | |
| System.out.println("close BufferedWriter"); | |
| super.close(); | |
| } | |
| } | |
| public static class FileWriterWrapper extends FileWriter { | |
| public FileWriterWrapper(File file) throws IOException { | |
| super(file); | |
| System.out.println("new FileWriter"); | |
| } | |
| @Override | |
| public void close() throws IOException { | |
| System.out.println("close FileWriter"); | |
| super.close(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment