openrewrite / openrewrite/rewrite-migrate-java

Java 11: use `Files.readString` to read a file to a String

Open
#91 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

guava recipe uses-dataflow
Dominant language
Java
Stars
156
Forks
130
Avg merge
20h 57m
Merged PRs (30d)
21

Description

There are many patterns to read all the contents of a File to a String

All the methods below will throw OOM if the file is larger than the available memory


// Java 7

String content = new String(Files.readAllBytes(Paths.get("file.txt")), StandardCharsets.UTF_8)`

// Using Guava 
import com.google.common.base.Charsets;
import com.google.common.io.Files;

String text = Files.toString(new File(path), Charsets.UTF_8);

// Using Files.readAllBytes

Path filePath = Path.of("file.txt");
String fileContent = "";

try
{
    byte[] bytes = Files.readAllBytes(Paths.get(filePath));
    fileContent = new String (bytes);
} 
catch (IOException e) 
{
    e.printStackTrace();
}

// Using BufferedReader (Java 6)

Path filePath = Path.of("file.txt");
String fileContent = "";

StringBuilder contentBuilder = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) 
{

    String sCurrentLine;
    while ((sCurrentLine = br.readLine()) != null) 
    {
        contentBuilder.append(sCurrentLine).append("\n");
    }
} 
catch (IOException e) 
{
    e.printStackTrace();
}

fileContent = contentBuilder.toString();

// Using Commons IO

File file = new File("file.txt");

String content = FileUtils.readFileToString(file, "UTF-8");

// Using the Scanner class

  File file = new File("file.txt");
  StringBuilder fileContents = new StringBuilder();        

    try (Scanner scanner = new Scanner(file)) {
        while(scanner.hasNextLine()) {
            fileContents.append(scanner.nextLine() + System.lineSeparator());
        }

    }

// Using the Scanner class with String concatenation

  File file = new File("file.txt");
  String fileContents = ""  

    try (Scanner scanner = new Scanner(file)) {
        while(scanner.hasNextLine()) {
            fileContents = fileContents  + System.lineSeparator());
        }
     
    }


Starting with Java 11, all the patterns above could be replaced with


Path filePath = Path.of("file.txt");
String content = Files.readString(fileName);

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue provides Java examples using Files.readAllBytes, BufferedReader, Scanner, and related APIs, but names no repository files or tests. Start by locating existing Java migration recipes that transform file-reading patterns, then determine how coverage is tested; done means the applicable patterns are migrated to Java 11 Files.readString with tests for the supported cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.