Java Rename File renameCanDelete()

Here you can find the source of renameCanDelete()

Description

rename Can Delete

License

Apache License

Declaration

private static boolean renameCanDelete() throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.IOException;

public class Main {
    private static boolean rcdDone = false;
    private static boolean rcdValue;

    private static boolean renameCanDelete() throws IOException {
        if (rcdDone)
            return rcdValue;
        File a = null;/*from   w w w.ja  va 2  s. c  om*/
        File b = null;
        try {
            a = File.createTempFile("java-rename-check-", "");
            b = File.createTempFile("java-rename-check-", "");
            rcdValue = a.renameTo(b);
            rcdDone = true;
        } finally {
            if (a != null)
                a.delete();
            if (b != null)
                b.delete();
        }
        return rcdValue;
    }

    public static boolean delete(File f) {
        if (f.isFile())
            return f.delete();
        else if (f.isDirectory()) {
            boolean b = clearDir(f);
            b &= f.delete();
            return b;
        } else
            return false;
    }

    public static boolean isFile(String fileName) {
        File testFile = new File(fileName);

        if ((testFile.exists()) && (testFile.isFile())) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isDirectory(String fileName) {
        File testFile = new File(fileName);

        if ((testFile.exists()) && (testFile.isDirectory())) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean clearDir(File dir) {
        if (!dir.isDirectory())
            return false;
        File[] files = dir.listFiles();
        if (files == null || files.length == 0)
            return true;
        boolean cleared = true;
        for (File sub : files) {
            cleared &= delete(sub);
        }
        return cleared;
    }
}

Related

  1. rename(String srcPath, String destPath)
  2. rename(String strFileName, String strRenameToName)
  3. rename(String was_name, String new_name)
  4. rename_file_to_file(String n1, String n2)
  5. renameBackup(File backupFile)
  6. renameContigs(File aliasFile, File cytobandFile, File seqDir)
  7. renameDirectory(File directory, File newDirectory)
  8. renameDirectory(File fromDir, File toDir)
  9. renameDirectory(String in_currentDirectoryPath, String in_newDirectoryPath)