I have a file that I\'ve been reading into a List via the following method:
List doc = java.nio.file.Files.readAllLines(new File(\"/path/to/src/res
An InputStream
represents a stream of bytes. Those bytes don't necessarily form (text) content that can be read line by line.
If you know that the InputStream
can be interpreted as text, you can wrap it in a InputStreamReader
and use BufferedReader#lines() to consume it line by line.
try (InputStream resource = Example.class.getResourceAsStream("resource")) {
List<String> doc =
new BufferedReader(new InputStreamReader(resource,
StandardCharsets.UTF_8)).lines().collect(Collectors.toList());
}
You can use Apache Commons IOUtils#readLines:
List<String> doc = IOUtils.readLines(inputStream, StandardCharsets.UTF_8);