Example usage for java.io File delete

List of usage examples for java.io File delete

Introduction

In this page you can find the example usage for java.io File delete.

Prototype

public boolean delete() 

Source Link

Document

Deletes the file or directory denoted by this abstract pathname.

Usage

From source file:Main.java

public static boolean deleteFile(String path) {
    if (TextUtils.isEmpty(path)) {
        return true;
    }//from w  w  w . j  a v a 2 s  .c  om

    File file = new File(path);
    if (!file.exists()) {
        return true;
    }
    if (file.isFile()) {
        return file.delete();
    }
    if (!file.isDirectory()) {
        return false;
    }

    File[] files = file.listFiles();
    if (files == null || files.length == 0) {
        return file.delete();
    }

    for (File f : files) {
        if (isFileExists(f)) {
            f.delete();
        } else if (f.isDirectory()) {
            deleteFile(f.getAbsolutePath());
        }
    }
    return file.delete();
}

From source file:Main.java

/**
 * Writes an element out to a file.//from   w w  w .j a  v  a 2  s  . c om
 * @param element The XML element to write out.
 * @param fileName The file name to write to. Existing file is overwriten.
 */
public static void writeElement(Element element, String fileName) {
    File file = new File(fileName);
    file.delete();
    DOMSource domSource = new DOMSource(element);
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file);
        StreamResult result = new StreamResult(fos);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(domSource, result);
        fos.flush();
        fos.close();
    } catch (Exception e) {
        throw new RuntimeException("Failed to write XML element to:" + fileName, e);
    } finally {
        try {
            fos.flush();
        } catch (Exception e) {
        }
        try {
            fos.close();
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

public static boolean delete(String path) {
    if (path == null || path.isEmpty())
        return false;
    File f = new File(path);
    if (f.exists()) {
        if (f.isDirectory()) {
            return deleteDirectory(f);
        } else {//from  www .  ja  va2s.c  o m
            return f.delete();
        }
    }
    return false;
}

From source file:com.mirth.connect.server.userutil.FileUtil.java

/**
 * Deletes a specified File. In Rhino and E4X 'delete' is a keyword, so File.delete() can't be
 * called within Mirth directly./*from   w w  w.ja  v  a 2 s  .c o  m*/
 * 
 * @param file
 *            The File to delete.
 * @return true if and only if the file or directory is successfully deleted; false otherwise
 * @throws SecurityException
 */
public static boolean deleteFile(File file) throws SecurityException {
    return file.delete();
}

From source file:net.fabricmc.installer.installer.LocalVersionInstaller.java

public static void installServer(File mcDir, IInstallerProgress progress) throws Exception {
    JFileChooser fc = new JFileChooser();
    fc.setDialogTitle(Translator.getString("install.client.selectCustomJar"));
    fc.setFileFilter(new FileNameExtensionFilter("Jar Files", "jar"));
    if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        File inputFile = fc.getSelectedFile();

        JarFile jarFile = new JarFile(inputFile);
        Attributes attributes = jarFile.getManifest().getMainAttributes();
        String fabricVersion = attributes.getValue("FabricVersion");
        jarFile.close();//  w w w  .j  a  va2s.  c o  m
        File fabricJar = new File(mcDir, "fabric-" + fabricVersion + ".jar");
        if (fabricJar.exists()) {
            fabricJar.delete();
        }
        FileUtils.copyFile(inputFile, fabricJar);
        ServerInstaller.install(mcDir, fabricVersion, progress, fabricJar);
    } else {
        throw new Exception("Failed to find jar");
    }

}

From source file:Main.java

public static boolean delFile(String appath, String filename) {
    if (checkDir(appath) == false) {
        return false;
    }/*from   www.  j  a  va  2  s .c o m*/
    File file = getFile(appath, filename);
    try {
        if (file.exists()) {
            return file.delete();
        } else {
            return false;
        }
    } catch (Exception ex) {
        return false;
    }
}

From source file:com.guye.baffle.util.OS.java

public static File createTempDirectory() throws BaffleException {
    try {// www  . j  a v a 2  s . c  om
        File tmp = File.createTempFile("BRUT", null);
        if (!tmp.delete()) {
            throw new BaffleException("Could not delete tmp file: " + tmp.getAbsolutePath());
        }
        if (!tmp.mkdir()) {
            throw new BaffleException("Could not create tmp dir: " + tmp.getAbsolutePath());
        }
        return tmp;
    } catch (IOException ex) {
        throw new BaffleException("Could not create tmp dir", ex);
    }
}

From source file:com.msd.gin.halyard.tools.HalyardUpdateTest.java

@BeforeClass
public static void setup() throws Exception {
    File rf = File.createTempFile("HalyardUpdateTest", "");
    rf.delete();
    rf.mkdirs();//from   w w  w .  jav  a  2 s.com
    ROOT = rf.toURI().toURL().toString();
    if (!ROOT.endsWith("/")) {
        ROOT = ROOT + "/";
    }
    ValueFactory vf = SimpleValueFactory.getInstance();
    HBaseSail sail = new HBaseSail(HBaseServerTestInstance.getInstanceConfig(), TABLE, true, 0, true, 0, null);
    sail.initialize();
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            sail.addStatement(vf.createIRI("http://whatever/subj" + i), vf.createIRI("http://whatever/pred"),
                    vf.createIRI("http://whatever/obj" + j));
        }
    }
    sail.commit();
    sail.shutDown();
    HalyardUpdate.conf = HBaseServerTestInstance.getInstanceConfig();
}

From source file:Main.java

public static void deleteRecursive(File fileOrDirectory) {
    if (fileOrDirectory.isDirectory()) {
        for (File child : fileOrDirectory.listFiles()) {
            deleteRecursive(child);/*from  w w  w  . j  a va  2s .c o m*/
        }
    }

    fileOrDirectory.delete();
}

From source file:com.ccoe.build.core.utils.FileUtils.java

public static boolean tryHardToDelete(File f) {
    if (!f.delete()) {
        if (ON_WINDOWS) {
            System.gc();//from  w  w  w  . j  a  v  a  2s  .c  om
        }
        try {
            Thread.sleep(DELETE_RETRY_SLEEP_MILLIS);
        } catch (InterruptedException ex) {
            // Ignore Exception
        }
        return f.delete();
    }
    return true;
}