I am trying to use streams to read a file but I cannot get past an exception. I have been looking around but I just can't understand why it's being thrown.
The file I am going to read is file.txt
and it's encoded with UTF-8.
I am reading it using Files.lines()
:
String path = FileWordCount.class.getResource("file.txt").getPath().substring(1);
Files.lines(Paths.get(path), Charset.forName("UTF-8")).forEach(System.out::println);
When trying to read the file I am getting the following exception:
Exception in thread "main" java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
[...]
Caused by: java.nio.charset.MalformedInputException: Input length = 1
Normally I don't post simple questions about exceptions but I just figure this one out.