# 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);
}

Use a Java Refactoring Tool

No license required

You can review this refactoring on your code without a license by installing jSparrow to your Eclipse IDE. Install the plug-in from Eclipse IDE: Eclipse Marketplace.

System-wide Refactoring

Do you want to automate this refactoring (and many more) to your system-wide code? The automatic application of this system-wide refactoring can be unlocked by acquiring your jSparrow license.

a drawn cute bird pointing at a graph that shows positive results

# Properties

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