Save as RTF And Read RTF

 

==================Save==============


    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        // TODO add your handling code here:

      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();


             try (FileOutputStream fos = new FileOutputStream(fileToSave)) {

                new RTFEditorKit().write(fos, textPane1.getDocument(), 0, textPane1.getDocument().getLength());

                System.out.println("File saved.");

            } catch (Exception ex) {

                ex.printStackTrace();

            }

            

            // 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(textPane1.getText());

//                fw.close();

//          } catch (IOException ex) {

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

//          }

            

        } else {

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

        }

        

    }                                        








===================Read====================


   private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        // TODO add your handling code here:

        JFileChooser fileChooser = new JFileChooser();

fileChooser.setDialogTitle("Open File");


int userSelection = fileChooser.showOpenDialog(null);


if (userSelection == JFileChooser.APPROVE_OPTION) {

    File selectedFile = fileChooser.getSelectedFile();

    StringBuilder contentBuilder = new StringBuilder();

   try (FileInputStream fis = new FileInputStream(selectedFile)) {

                textPane1.setDocument(new DefaultStyledDocument());

                new RTFEditorKit().read(fis, textPane1.getDocument(), 0);

                System.out.println("File loaded.");

            } catch (Exception ex) {

                ex.printStackTrace();

            }

//    try (BufferedReader reader = new BufferedReader(new FileReader(selectedFile))) {

//        String line;

//        while ((line = reader.readLine()) != null) {

//            contentBuilder.append(line).append("\n");

//        }

//        textPane1.setText(contentBuilder.toString()); // Show content in textPane1

//        System.out.println("File loaded: " + selectedFile.getAbsolutePath());

//    } catch (IOException ex) {

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

//    }

} else {

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

}


    }                                        


Comments

Popular posts from this blog

Remove (Clear) and remove Selected element

JSpinner

🛢️ MySQL Connection & CRUD Operations in Java