gpt4 book ai didi

java - 如何使用 JFileChooser 将 .txt 文件保存在所需位置

转载 作者:行者123 更新时间:2023-12-02 00:02:56 25 4
gpt4 key购买 nike

使用以下代码我可以将program.txt存储在工作项目文件夹中,但是如何使用JFileChooser或任何其他选项将文件保存在选定位置?

b2.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
try {

o = new BufferedWriter(new FileWriter("program.txt"));

o.write(t1.getText());
o.write(",");
o.write(t2.getText());
o.write(",");
o.write(t3.getText());
o.write(",");
o.write(t4.getText());
o.write(",");
o.write(t5.getText());
o.write(",");
o.write(t6.getText());
o.write(",");
o.write(t7.getText());
o.write(",");
o.close();


}
catch (IOException e) {
e.printStackTrace();
}
}
});

最佳答案

final JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(aComponent); //parent component to JFileChooser
if (returnVal == JFileChooser.APPROVE_OPTION) { //OK button pressed by user
File file = fc.getSelectedFile(); //get File selected by user
o = new BufferedWriter(new FileWriter(file)); //use its name

...
//your writing code goes here
}

关于java - 如何使用 JFileChooser 将 .txt 文件保存在所需位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14479988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com