Arquivo de leitura Java 8 usando fluxos java.io.UncheckedIOException [duplicado]
Esta pergunta já tem uma resposta aqui:
Files.readAllBytes vs Files.lines obtendo MalformedInputException 4 respostasEstou tentando usar fluxos para ler um arquivo, mas não consigo passar por uma exceção. Eu tenho olhado em volta, mas simplesmente não consigo entender por que está sendo jogado.
O arquivo que vou ler éfile.txt
e é codificado com UTF-8.
Estou lendo usandoFiles.lines()
:
String path = FileWordCount.class.getResource("file.txt").getPath().substring(1);
Files.lines(Paths.get(path), Charset.forName("UTF-8")).forEach(System.out::println);
Ao tentar ler o arquivo, estou recebendo a seguinte exceção:
Exception in thread "main" java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
[...]
Caused by: java.nio.charset.MalformedInputException: Input length = 1
Normalmente eu não postei perguntas simples sobre exceções, mas apenas resolvo isso.