Не сохраняется текст в word, нужна помощь
Суть программы в том, чтобы сохранять все данные введенные пользователем (добавленные в список) и выводить их в word файл. При запуске программы выводится текст в docx файле, при открытии самого файла все данные на месте. НО при следующих запусках текст стирается и выводится совершенно новый. Я добавил блок if, в котором создаю новый параграф, если файл уже создан (не помогло)
public class Main {
public static void main(String[] args) throws IOException {
contactForm form = new contactForm();
form.setVisible(true);
}
}
public class contactForm extends JFrame {
JTextField name_field, number_field;
public contactForm() {
super("Анкета");
super.setBounds(250, 250, 600, 200);
super.setDefaultCloseOperation(EXIT_ON_CLOSE);
Container container = super.getContentPane();
container.setLayout(new GridLayout(5, 2, 2, 10));
JLabel name = new JLabel("ФИО");
name_field = new JTextField("");
JLabel number = new JLabel("Номер телефона: ");
number_field = new JTextField("+");
container.add(name);
container.add(name_field);
container.add(number);
container.add(number_field);
JButton send = new JButton("Добавить контакт");
container.add(send);
send.addActionListener(new ButtonEventManager());
}
class ButtonEventManager implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String[] nmbArray = {};
ArrayList<String> numberandnames = new ArrayList<>((Arrays.asList(nmbArray)));
numberandnames.add(name_field.getText());
numberandnames.add(number_field.getText());
Path link = Paths.get("C:\\Users\\Admin\\Desktop\\BBB.docx");
XWPFDocument docxModel = new XWPFDocument();
if (Files.exists(link)) {
docxModel.createParagraph();
String documentLine = docxModel.getDocument().toString();
CTSectPr ctSectPr = docxModel.getDocument().getBody().addNewSectPr();
XWPFParagraph bodyParagraph = docxModel.createParagraph();
bodyParagraph.setAlignment(ParagraphAlignment.LEFT);
XWPFRun paragraphConfig = bodyParagraph.createRun();
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
paragraphConfig.setItalic(true);
paragraphConfig.setFontSize(20);
paragraphConfig.setColor("170101");
paragraphConfig.setFontSize(12);
paragraphConfig.setText(numberandnames.toString());
try {
FileOutputStream outputStream = new FileOutputStream("C:\\Users\\Admin\\Desktop\\BBB.docx");
docxModel.write(outputStream);
outputStream.close();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
dispose();
contactForm.Word wrd = new contactForm.Word();
wrd.setVisible(true);
}
}
}
static class Word extends JFrame {
public Word() {
super("Анкета");
// Выход из программы при закрытии
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Кнопки для создания диалоговых окон
JButton docx = new JButton(".docx");
docx.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
try {
desktop.open(new File("C:\\Users\\Admin\\Desktop\\BBB.docx"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
});
// Создание панели содержимого с размещением кнопок
JPanel contents = new JPanel();
contents.add(docx);
setContentPane(contents);
// Определение размера и открытие окна
setSize(350, 100);
}
private JDialog createDialog(String title, boolean modal) {
final JDialog dialog = new JDialog(this, title, modal);
dialog.setDefaultCloseOperation(EXIT_ON_CLOSE);
dialog.setSize(180, 90);
return dialog;
}
}
}