import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.function.Function; import java.util.stream.Stream; class Scratch { public static void main(String[] args) { Function first = Files.lines(Paths.get("test.txt")).map(e -> wrapFunction(Scratch::processLine)).toList(); Stream stringStream = Files.lines(Paths.get("test.txt")).map(wrapFunction(Scratch::processLine)); } public static Function wrapFunction(CheckedExceptionFunction checkedExceptionFunction) { return t -> { try { return checkedExceptionFunction.apply(t); } catch (Exception e) { throw new RuntimeException(e); } }; } private static String processLine(String line) throws IOException { if (line.equals("error")) { throw new IOException("エラーが発生しました"); } return line.toUpperCase(); } @FunctionalInterface interface CheckedExceptionFunction { R apply(T t) throws E; } }