# Use Files.writeString

# Description

Java 11 introduced Files.writeString(Path, CharSequence, Charset, OpenOption...) (opens new window) and Files.writeString(Path, CharSequence, OpenOption...) (opens new window) for writing text into a file by one single invocation and in an efficient non-blocking manner.

This rule replaces BufferedWriters that are used to write a single value into a file, with Files.write(...).

# Benefits

Achieves better performance for writing small files. Additionally, improves readability by removing code clutter.

# Tags

# Code Changes

# BufferedWriter Created with FileWriter

Pre

try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("/home/test/testpath"))) {
  bufferedWriter.write("Hello World!");
} catch (IOException ioException) {
  logError("File could not be written.", ioException);
}

Post

try {
  Files.writeString(Paths.get("/home/test/testpath"), "Hello World!", Charset.defaultCharset());
} catch (IOException ioException) {
  logError("File could not be written.", ioException);
}

# Specifying Charset in BufferedWriter

Pre

try (BufferedWriter bufferedWriter = new BufferedWriter(
    new FileWriter("/home/test/testpath", StandardCharsets.UTF_8))) {
  bufferedWriter.write("Hello World!");
} catch (IOException ioException) {
  logError("File could not be written.", ioException);
}

Post

try {
  Files.writeString(Paths.get("/home/test/testpath"), "Hello World!", StandardCharsets.UTF_8);
} catch (IOException ioException) {
  logError("File could not be written.", ioException);
}

# BufferedWriter Initialized with Files.newBufferedWriter

Pre

try (BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get("/home/test/testpath"))) {
  bufferedWriter.write("Hello World!");
} catch (IOException ioException) {
  logError("File could not be written.", ioException);
}

Post

try {
  Files.writeString(Paths.get("/home/test/testpath"), "Hello World!");
} catch (IOException ioException) {
  logError("File could not be written.", ioException);
}

# Files.newBufferedWriter with Path and Charset

Pre

try (BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get("/home/test/testpath"),
    StandardCharsets.UTF_8)) {
  bufferedWriter.write("Hello World!");
} catch (IOException ioException) {
  logError("File could not be written.", ioException);
}

Post

try {
  Files.writeString(Paths.get("/home/test/testpath"), "Hello World!", StandardCharsets.UTF_8);
} catch (IOException ioException) {
  logError("File could not be written.", ioException);
}

# Files.newBufferedWriter with Path, Charset, and OpenOption

Pre

try (BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get("/home/test/testpath"),
    StandardCharsets.UTF_8, StandardOpenOption.APPEND)) {
  bufferedWriter.write("Hello World!");
} catch (IOException ioException) {
  logError("File could not be written.", ioException);
}

Post

try {
  Files.writeString(Paths.get("/home/test/testpath"), "Hello World!", StandardCharsets.UTF_8,
      StandardOpenOption.APPEND);
} catch (IOException ioException) {
  logError("File could not be written.", ioException);
}

🛠️ Auto-refactor Available

You can auto-refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:

Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client

Need help? Check out our installation guide.

# Properties

Property Value
Rule ID UseFilesWriteString
First seen in jSparrow version 3.24.0
Minimum Java version 11
Remediation cost 5 min