Java 11: use `Files.readString` to read a file to a String
维护者通常 1 天内回复
@Ouokki 已经在做这个了。
开始于 2026年6月17日。
评估
调研方向
该 issue 提供了使用 Files.readAllBytes、BufferedReader、Scanner 及相关 API 的 Java 示例,但没有指出任何仓库文件或测试。首先定位用于转换文件读取模式的现有 Java 迁移 recipe,然后确定如何测试覆盖率;当适用的模式已迁移到 Java 11 Files.readString,并且已为受支持的情况编写测试时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
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 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
openrewrite/rewrite-migrate-java 的其他 Issue
-
bug
难度 3/5 半天 新手友好度 40/100
openrewrite/rewrite-migrate-java#1292 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 55/100
openrewrite/rewrite-migrate-java#1289 ·
维护者通常 1 天内回复
-
bug
难度 4/5 3-5 天 新手友好度 68/100
openrewrite/rewrite-migrate-java#1246 ·
维护者通常 1 天内回复
-
难度 5/5 一周以上 新手友好度 32/100
openrewrite/rewrite-migrate-java#1227 · 2 条评论 ·
维护者通常 1 天内回复
-
OptionalNotPresentToIsEmpty rewrites `!isPresent()` on a non-`java.util.Optional` type, producing uncompilable `isEmpty()`可能重新可做 关联的 PR 已关闭且未合并。 未关闭
难度 3/5 1-2 天 新手友好度 70/100
openrewrite/rewrite-migrate-java#1146 · 3 条评论 ·
维护者通常 1 天内回复
查看 openrewrite/rewrite-migrate-java 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 62/100
NationalSecurityAgency/ghidra#9884 ·
维护者通常 2 天内回复
-
Status: Pending Review Type: Feature Request
难度 1/5 1 小时以内 新手友好度 82/100
OpenRefine/OpenRefine#8019 ·
维护者通常 1 天内回复
-
`timer` rule rejects dates the engine accepts可能已有人在做 @MFA-G 今天认领。 未关闭bug ready
难度 2/5 1-3 小时 新手友好度 72/100
camunda/bpmnlint-plugin-camunda-compat#281 · 3 条评论 ·
维护者通常 1 天内回复
-
status: waiting-for-triage
难度 2/5 1-3 小时 新手友好度 72/100
-
bug
难度 2/5 1-3 小时 新手友好度 62/100