java以相反的顺序在textarea文本文件中打印

时间:2018-11-09 08:31:41

标签: java

我正在使用此代码,它可以很好地运行,但是我需要在每行中添加“ \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);
    }                
} 

2 个答案:

答案 0 :(得分:1)

您可以在源代码处变换每一行,这意味着在要收集的行之前添加一个地图指令:

lines.map(element -> element + '\n').collect(Collectors.toCollection(LinkedList::new)).descendingIterator()
                .forEachRemaining(testBuilder::append);

答案 1 :(得分:0)

只需尝试更改

.forEachRemaining(jTextArea1::append);

收件人:

.forEachRemaining(singleLine -> jTextArea1.append(singleLine  + "\n"));