Save File Dialog

     JFileChooser fileChooser = new JFileChooser();

        fileChooser.setDialogTitle("Save As");


        int userSelection = fileChooser.showSaveDialog(null);


        if (userSelection == JFileChooser.APPROVE_OPTION) {

            File fileToSave = fileChooser.getSelectedFile();

            String filePath = fileToSave.getAbsolutePath();


            // Add .sabir extension if it's not present

            if (!filePath.toLowerCase().endsWith(".sabir")) {

                filePath += ".sabir";

                fileToSave = new File(filePath);

            }


            System.out.println("Save file to: " + fileToSave.getAbsolutePath());

            

           //  Now you can write to this file

             FileWriter fw;

          try {

              fw = new FileWriter(fileToSave);

               fw.write("your content here");

                fw.close();

          } catch (IOException ex) {

              Logger.getLogger(DashBoard.class.getName()).log(Level.SEVERE, null, ex);

          }

            

        } else {

            System.out.println("Save command cancelled by user.");

        }

        

Comments

Popular posts from this blog

Remove (Clear) and remove Selected element

JSpinner

🛢️ MySQL Connection & CRUD Operations in Java