0

使用以下代码,我可以将 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();
            }
        }
    });
4

2 回答 2

3
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
}
于 2013-01-23T12:50:05.243 回答
0

您可以通过在 JFileChooser 对象中设置 File 对象来做到这一点

File f = new File("filename");
myJFileChooser.setSelectedFile(f);

查看这篇文章了解更多

http://www.coderanch.com/t/561950/GUI/java/Save-JTextArea-JFileChooser-TXT-file

于 2013-01-23T12:46:10.270 回答