Java 11: use `Files.readString` to read a file to a String
Mantenedores costumam responder em até 1 dia
@Ouokki já está trabalhando nisso.
Desde 17/6/2026.
Avaliação
- Dificuldade
- 3/5
- Tempo estimado
- 1-2 dias
- Facilidade para iniciantes
- 42/100
Direção de pesquisa
A issue fornece exemplos em Java usando Files.readAllBytes, BufferedReader, Scanner e APIs relacionadas, mas não nomeia arquivos nem testes do repositório. Comece localizando as receitas de migração Java existentes que transformam padrões de leitura de arquivos e, em seguida, determine como a cobertura é testada; considera-se concluído quando os padrões aplicáveis tiverem sido migrados para Java 11 Files.readString e houver testes para os casos compatíveis.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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);
- Linguagem predominante
- Java
- Estrelas
- 160
- Forks
- 133
- Merge médio
- 4h 52min
- PRs com merge (30d)
- 105
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Sem modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de openrewrite/rewrite-migrate-java
-
good first issue
Dificuldade 4/5 3-5 dias Facilidade para iniciantes 30/100
openrewrite/rewrite-migrate-java#1330 · 2 comentários ·
Mantenedores costumam responder em até 1 dia
-
UseMapOf: Skip refactoring large maps to Map.ofEntries to prevent javac compiler hangs (add maxEntries option)Talvez já em andamento @Yuren0312 assumiu há 1 dia. Abertabug
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 55/100
openrewrite/rewrite-migrate-java#1310 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
-
bug
Dificuldade 4/5 3-5 dias Facilidade para iniciantes 55/100
openrewrite/rewrite-migrate-java#1307 ·
Mantenedores costumam responder em até 1 dia
-
bug
Dificuldade 4/5 3-5 dias Facilidade para iniciantes 68/100
openrewrite/rewrite-migrate-java#1246 ·
Mantenedores costumam responder em até 1 dia
-
Feedback from a 152k-line Java 8 to 25 migration: the silent behavioural changes that cost the most, and which have no recipeTalvez já em andamento Um pull request vinculado a esta issue está aberto ou já foi mesclado. Aberta
Dificuldade 5/5 Mais de uma semana Facilidade para iniciantes 32/100
openrewrite/rewrite-migrate-java#1227 · 4 comentários · 1 reação ·
Mantenedores costumam responder em até 1 dia
Todas as issues de openrewrite/rewrite-migrate-java
Issues semelhantes
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 68/100
strimzi/strimzi-kafka-operator#13328 · 1 comentário · 3 reações ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 74/100
GoogleContainerTools/jib#4538 ·
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 67/100
OpenLiberty/ci.maven#2137 ·
Mantenedores costumam responder em até 1 dia
-
Payer adjustment can exceed the balance left after copay and deductible, so claim payments add up to more than the chargeTalvez já em andamento @DG47 assumiu hoje. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 79/100
synthetichealth/synthea#1772 ·
Mantenedores costumam responder em até 13 dias
-
bug dependencies
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 65/100