/** * Reads all lines of text from this source, running the given {@code action} for each line as it * is read. * * <p>Like {@link BufferedReader#readLine()}, this method considers a line to be a sequence of * text that is terminated by (but does not include) one of {@code \r\n}, {@code \r} or {@code * \n}. If the source's content does not end in a line termination sequence, it is treated as if * it does. * * @throws IOException if an I/O error occurs while reading from this source or if {@code action} * throws an {@code UncheckedIOException} * @since 22.0 */ @Beta public void forEachLine(Consumer<? super String> action) throws IOException { try (Stream<String> lines = lines()) { // The lines should be ordered regardless in most cases, but use forEachOrdered to be sure lines.forEachOrdered(action); } catch (UncheckedIOException e) { throw e.getCause(); } }
/** * Reads all lines of text from this source, running the given {@code action} for each line as it * is read. * * <p>Like {@link BufferedReader#readLine()}, this method considers a line to be a sequence of * text that is terminated by (but does not include) one of {@code \r\n}, {@code \r} or {@code * \n}. If the source's content does not end in a line termination sequence, it is treated as if * it does. * * @throws IOException if an I/O error occurs while reading from this source or if {@code action} * throws an {@code UncheckedIOException} * @since 22.0 */ @Beta public void forEachLine(Consumer<? super String> action) throws IOException { try (Stream<String> lines = lines()) { // The lines should be ordered regardless in most cases, but use forEachOrdered to be sure lines.forEachOrdered(action); } catch (UncheckedIOException e) { throw e.getCause(); } }
public void testLines() throws IOException { try (Stream<String> lines = source.lines()) { assertExpectedLines(lines.collect(toImmutableList())); } }
private static ImmutableList<String> loadStrings(String path) throws IOException { try (Stream<String> lines = Files.asCharSource(new File(path), Charsets.UTF_8).lines()) { return lines .map(line -> line.trim()) .filter(line -> !line.isEmpty()) .collect(toImmutableList()); } } }
/** * Reads all lines of text from this source, running the given {@code action} for each line as it * is read. * * <p>Like {@link BufferedReader#readLine()}, this method considers a line to be a sequence of * text that is terminated by (but does not include) one of {@code \r\n}, {@code \r} or {@code * \n}. If the source's content does not end in a line termination sequence, it is treated as if * it does. * * @throws IOException if an I/O error occurs while reading from this source or if {@code action} * throws an {@code UncheckedIOException} * @since 22.0 */ @Beta public void forEachLine(Consumer<? super String> action) throws IOException { try (Stream<String> lines = lines()) { // The lines should be ordered regardless in most cases, but use forEachOrdered to be sure lines.forEachOrdered(action); } catch (UncheckedIOException e) { throw e.getCause(); } }
/** * Reads all lines of text from this source, running the given {@code action} for each line as it * is read. * * <p>Like {@link BufferedReader#readLine()}, this method considers a line to be a sequence of * text that is terminated by (but does not include) one of {@code \r\n}, {@code \r} or {@code * \n}. If the source's content does not end in a line termination sequence, it is treated as if * it does. * * @throws IOException if an I/O error occurs while reading from this source or if {@code action} * throws an {@code UncheckedIOException} * @since 22.0 */ @Beta public void forEachLine(Consumer<? super String> action) throws IOException { try (Stream<String> lines = lines()) { // The lines should be ordered regardless in most cases, but use forEachOrdered to be sure lines.forEachOrdered(action); } catch (UncheckedIOException e) { throw e.getCause(); } }
public void testLines() throws IOException { try (Stream<String> lines = source.lines()) { assertExpectedLines(lines.collect(toImmutableList())); } }