[英]java print in textarea text file in reverse order
I'm using this code, it runs well but i need to add " \\n
" to each line我正在使用此代码,它运行良好,但我需要在每一行中添加“
\\n
”
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
filename = "Reverse.txt";
file = new File(filename);
try (final Stream<String> lines = Files.lines(Paths.get(filename))) {
lines.collect(Collectors.toCollection(LinkedList::new))
.descendingIterator()
.forEachRemaining(jTextArea1::append); // <<<<<<< need "\n"
}
catch (IOException ex) {
Logger.getLogger(TextAreaReverseReadFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
You can transform each line at source, meaning adding a map instruction just before the collect one:您可以在源代码中转换每一行,这意味着在 collect 之前添加一个 map 指令:
lines.map(element -> element + '\n').collect(Collectors.toCollection(LinkedList::new)).descendingIterator()
.forEachRemaining(testBuilder::append);
Simply try to change简单地尝试改变
.forEachRemaining(jTextArea1::append);
to:到:
.forEachRemaining(singleLine -> jTextArea1.append(singleLine + "\n"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.