将数组写入java中textArea的文本文件

时间:2015-03-15 21:49:15

标签: java arrays text-files jtextarea

我是Java的新手,我正在尝试将名为AreaBooking的文本区域中的内容编写到文本文件中。我可以让它写入文本文件,但不能让它们分开。我也可能有很多冗余的代码,但我不知道,因为我说我是这种语言的新手。

我希望它像

1
2
3

而不是1,2,3

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                          
    AreaBooking.replaceSelection("");      

    String s[] = AreaBooking.getText().split("\n");
    ArrayList<String>arrList = new ArrayList<>(Arrays.asList(s)) ;
    System.out.println(arrList);


    PrintWriter out;
    try {
        out = new PrintWriter(new BufferedWriter(new FileWriter(file_name)));
        out.write(AreaBooking.getText());
        out.close();
    } catch (IOException ex) {
        Logger.getLogger(ViewBookings.class.getName()).log(Level.SEVERE, null, ex);
    }

2 个答案:

答案 0 :(得分:1)

而不是

    out.write(AreaBooking.getText());

写:

for (String s : arrList) {
    out.println(s);
}

答案 1 :(得分:1)

改为使用JTextArea#write(Writer)

try (Writer writer = new BufferedWriter(new WriterReader("Inventory.txt"))) {
    textArea.write(writer);
} catch (IOException exp) {
    exp.printStackTrace();
}