Java JFrame deleteFile(String filepath, JFrame parent)

Here you can find the source of deleteFile(String filepath, JFrame parent)

Description

Deletes the given file

License

Open Source License

Parameter

Parameter Description
filepath - path of file to delete
parent - parent component

Return

boolean

Declaration

public static boolean deleteFile(String filepath, JFrame parent) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import javax.swing.*;

import java.io.*;

public class Main {
    /**/*from  ww w .  ja v a 2s .co  m*/
     * Deletes the given file
     * @param filepath - path of file to delete
     * @param parent - parent component
     * @return boolean
     */
    public static boolean deleteFile(String filepath, JFrame parent) {
        File tempFile = new File(filepath);
        File trashFile = new File(ClassLoader.getSystemResource("trash")
                .getPath(), new File(filepath).getName());
        if (trashFile.exists())
            recursiveDeleteFile(trashFile);

        if (tempFile.exists() && !tempFile.renameTo(trashFile)) {
            String errstr = "Unable to delete module.. Please check if trash path exists...";
            JOptionPane.showMessageDialog(parent, errstr,
                    "File Delete Error", JOptionPane.ERROR_MESSAGE);
            return false;
        } else
            return true;
    }

    public static void recursiveDeleteFile(File f) {
        File[] children = f.listFiles();
        if (children != null) {
            for (int i = 0; i < children.length; i++)
                recursiveDeleteFile(children[i]);
        }
        f.delete();
    }
}

Related

  1. confirm(JFrame parent, String query, int type)
  2. createConfirmOnExitAdapter(final JFrame frame, final String title, final String message)
  3. createDialogForPanel(JFrame parent, JPanel panel)
  4. createPackedJFrame( java.awt.Component content, String title, int closeOperation)
  5. decorateFrame(JFrame frame, JMenuBar menuBar)
  6. dialogYesNo(JFrame frame, String title, String message, Object[] options)
  7. dispAlert(JFrame pFrame, String pTask, String pMessaggio)
  8. dispErrore(JFrame frame, String task, String mess)
  9. display(JFrame parent, JInternalFrame dialog)