Without any additional helper functions or external libraries, the easiest way is:
Stream<String> lines1 = Files.lines(Paths.get("/somepathtofile"));
Stream<String> lines2 = Files.lines(Paths.get("/somepathtoanotherfile"));
Stream.concat(lines1, lines)
.filter(...)
.forEach(...);
If Files.linesit was not announced that he had thrown an excluded exception, you can make
Stream.of("/file1", "/file2")
.map(Paths::get)
.flatMap(Files::lines)....
, , . . , Files.lines, , IOException UncheckedIOException. - , . :
@FunctionalInterface
public interface ThrowingFunction<T,R> extends Function<T,R> {
@Override
public default R apply(T t) {
try {
return throwingApply(t);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static<T,R> Function<T,R> wrap(ThrowingFunction<T,R> f) {
return f;
}
R throwingApply(T t) throws Exception;
}
Stream.of("/somefile", "/someotherfile", "/yetanotherfile")
.map(Paths::get)
.flatMap(ThrowingFunction.wrap(Files::lines))
.....
, - .