我正在尝试建立一个表单。在表单的末尾,我喜欢使用“保存”按钮来创建一个以该人在表单中输入的姓氏命名的TXT文件。 我只成功创建了一个带有已定义名称(我选择)的txt文件或没有带有姓氏信息的扩展名的文件。这是代码,谢谢大家的关注:
Button btnSalva = new Button(shell, SWT.NONE);
btnSalva.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(text_1.getText()));
writer.write("Nome" + text.getText() + "Cognome" + text_1.getText() + "età" + text_2.getText());
} catch (IOException e1) {
} finally {
try {
if (writer != null)
writer.close();
} catch (IOException e1) {
}
}
}
});
btnSalva.setBounds(10, 799, 75, 25);
btnSalva.setText("SALVA");
答案 0 :(得分:0)
如果姓氏是你的text_1.getText(),并且你想要一个TXT扩展,那么只需更改你的行:
writer = new BufferedWriter(new FileWriter(text_1.getText()));
进入
writer = new BufferedWriter( new FileWriter( text_1.getText() + "TXT" ));
答案 1 :(得分:0)
我解决了这个问题:
writer = new BufferedWriter( new FileWriter( text_1.getText() + ".TXT" ));