Answer the question
In order to leave comments, you need to log in
Why does it give a compilation error?
Please tell me why in this code:
public void write() throws IOException {
String splitter = String.join("", Collections.nCopies(settings.page.width, "-"));
List<String> headLines = columnsToRows(getColumnTitles());
try (BufferedWriter writer = Files.newBufferedWriter(outputPath, DEFAULT_CHARSET)) {
inputData.stream().map(this::columnsToRows).flatMap(List::stream).forEach(writer::write);
}
}
Answer the question
In order to leave comments, you need to log in
In short, I did this:
public void write() throws IOException {
List<String> columnTitles = settings.columns.stream().map(x -> x.title).collect(Collectors.toList());
String splitter = String.join("", Collections.nCopies(settings.page.width, "-"));
List<String> headLines = columnsToRows(columnTitles);
try (BufferedWriter writer = Files.newBufferedWriter(outputPath, DEFAULT_CHARSET)) {
Stream<String> lines = inputData.stream().flatMap(x -> columnsToRows(x).stream().limit(columnTitles.size()));
Iterator<String> iterator = lines.iterator();
while (iterator.hasNext()) {
String line = iterator.next();
writer.write(line);
}
}
}
Alternatively, you could throw an unchecked exception:
public void write() {
String splitter = String.join("", Collections.nCopies(settings.page.width, "-"));
List<String> headLines = columnsToRows(getColumnTitles());
try (BufferedWriter writer = Files.newBufferedWriter(outputPath, DEFAULT_CHARSET)) {
inputData.stream().map(this::columnsToRows).flatMap(List::stream).forEach(writer::write);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question