Modernize BufferedWriter creation and prevent file descriptor leaks

MSAL

The code new BufferedWriter(new FileWriter(f)) creates a BufferedWriter that does not close the underlying FileWriter when it is closed. This can lead to file descriptor leaks as per CWE-755. Use Files.newBufferedWriter to create a BufferedWriter that closes the underlying file descriptor when it is closed.

org.openrewrite.staticanalysis.BufferedWriterCreationRecipes
mod run . --recipe=org.openrewrite.staticanalysis.BufferedWriterCreationRecipes

Install it once from Recipe actions, then run it on every repository under the current directory. New to the Moderne CLI? Set it up.

Recipe list

It runs these recipes, in this order.

  1. Convert new BufferedWriter(new FileWriter(File)) to Files.newBufferedWriter(Path)
  2. Convert new BufferedWriter(new FileWriter(String)) to Files.newBufferedWriter(Path)
  3. Convert new BufferedWriter(new FileWriter(File, boolean)) to Files.newBufferedWriter(Path, StandardOpenOption)
  4. Convert new BufferedWriter(new FileWriter(String, boolean)) to Files.newBufferedWriter(Path, StandardOpenOption)