Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

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

未关闭
#91 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@Ouokki 已经在做这个了。

开始于 2026年6月17日。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
42/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
冷清
技术栈
java
领域
tooling

调研方向

该 issue 提供了使用 Files.readAllBytes、BufferedReader、Scanner 及相关 API 的 Java 示例,但没有指出任何仓库文件或测试。首先定位用于转换文件读取模式的现有 Java 迁移 recipe,然后确定如何测试覆盖率;当适用的模式已迁移到 Java 11 Files.readString,并且已为受支持的情况编写测试时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

guava recipe uses-dataflow

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

主要语言
Java
星标
156
派生
129
平均合并
7 小时 12 分钟
30 天内合并 PR
43

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

openrewrite/rewrite-migrate-java 的其他 Issue

查看 openrewrite/rewrite-migrate-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。