Affichage des articles dont le libellé est java remove file. Afficher tous les articles
Affichage des articles dont le libellé est java remove file. Afficher tous les articles

Java Create, Write, Delete Text File

How To Create And Write And Delete Txt File Using Java NetBeans

java create writr remove text file



In this Java tutorial will demonstrate how to create a text file with a custom name and write data from a jTextArea into the file. Additionally, we will learn how to remove the file using the FileWriter and BufferedWriter classes in Java NetBeans, all in a step-by-step manner .




Project Source Code:


// create and write in text file
private void jButtonCreateAndWriteActionPerformed(java.awt.event.ActionEvent evt) {                                                      
      
        String filePath = jTextFieldFilePath.getText();
        
        File file = new File(filePath);
        
        if(!file.exists()){
            try {
                
                file.createNewFile();
                FileWriter wr = new FileWriter(file);
                BufferedWriter bw = new BufferedWriter(wr);
                
                bw.write(jTextArea1.getText());
                
                bw.close();
                wr.close();
                
            } catch (IOException ex) {
                Logger.getLogger(TxtFile_Create_Write_Delete.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }                                                     

   // delete file
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
        String filePath = jTextFieldFilePath.getText();
        
        File file = new File(filePath);
        
        if(file.exists()){
            file.delete();
        }
        
    } 


OutPut:

create write delete text file using java



Java - Remove File And Directory

How to Delete File And Folder Using Java

java remove directory



In this Java Tutorial we will see How To Delete File Or A Folder From A Path String Using Java NetBeans .




Project Source Code:


package javaapplication1;

import java.io.File;
import javax.swing.JOptionPane;

public class Java_Remove_File_Or_Directory extends javax.swing.JFrame {

    /**
     * Creates new form Java_Remove_File_Or_Directory
     */
    public Java_Remove_File_Or_Directory() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jTextFieldPath = new javax.swing.JTextField();
        BTN_Remove = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextFieldPath.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

        BTN_Remove.setText("Delete");
        BTN_Remove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTN_RemoveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTextFieldPath, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(BTN_Remove, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(BTN_Remove)
                    .addComponent(jTextFieldPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(84, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void BTN_RemoveActionPerformed(java.awt.event.ActionEvent evt) {                                           
        
        File file = new File(jTextFieldPath.getText());
        
        if(file.exists())
        {
            if(file.isDirectory())
            {
                // get the files inside the folder
                File[] fs = file.listFiles();
                for(File f : fs)
                {
                    // remove files
                    f.delete();
                }
                // remove the folder
                file.delete();
                JOptionPane.showMessageDialog(null, "Directory Deleted");
            }
            else
            {
                file.delete();
                JOptionPane.showMessageDialog(null, "File Deleted");
            }
            
        }
        
        
    }                                          

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Java_Remove_File_Or_Directory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Java_Remove_File_Or_Directory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Java_Remove_File_Or_Directory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Java_Remove_File_Or_Directory.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Java_Remove_File_Or_Directory().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton BTN_Remove;
    private javax.swing.JTextField jTextFieldPath;
    // End of variables declaration                   
}