Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
1 つのファイルを複数のファイルに書き込む Java プログラムがありますが、すべての接続が同時に開いているわけではありません。
プログラムが強制終了されたときに (コマンド ラインで ctrl-c を使用して)、これらの作成されたファイルをすべて削除する方法を追加したいと考えています。
シャットダウンフックの追加は、オブジェクトがシャットダウンされるときに常に実行されます。プログラムが強制終了された場合にのみ削除したいです。キャッチできる RuntimeException はありますか?
をご覧くださいRuntime.getRuntime().addShutdownHook()。これが例です。
Runtime.getRuntime().addShutdownHook()