简体   繁体   English

在任何位置使用JFileChooser保存文件

[英]saving file using JFileChooser at any location

hello i am trying to save the file at location specified by Jfilechooser the situation now is when user click on save the file is created on the project direction with the name title but what i need is to give the user option where to save the file and choose the name he like using JFileChooser 你好,我正在尝试将文件保存在Jfilechooser指定的位置,现在的情况是,当用户单击“保存”时,将在项目方向上以名称标题创建文件,但是我需要给用户选择保存文件的位置,并且使用JFileChooser选择他喜欢的名称

if(buttonPressed.equals(save)){

         File myFile = new File("title");
         final JFileChooser fc = new JFileChooser();
         int returnVal = fc.showSaveDialog(null);
         fc.setSelectedFile(myFile);
         if (returnVal == JFileChooser.APPROVE_OPTION) {

            try (PrintWriter writer = new PrintWriter(myFile)) {   

            for (int i =0 ; i< cModel.getRowCount(); i++)
            {
               sData.add(cModel.getValueAt(i, 1).toString());
               eData.add(cModel.getValueAt(i, 2).toString());
               tData.add(cModel.getValueAt(i, 3).toString());
               writer.println(i+1 + "\n");
               writer.println(sData.get(i)+" --> " + eData.get(i));
               writer.println(tData.get(i));
               writer.println("\n");
            }
            }catch(FileNotFoundException ioEx){

            } 

         }

         }

Try 尝试

JFrame choose = new JFrame();
choose.setTitle("Save To ...");
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(choose);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM