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

private static boolean checkFsWritable() {
    String directoryName = Environment.getExternalStorageDirectory().toString() + "/DCIM";
    File directory = new File(directoryName);
    if (!directory.isDirectory()) {
        if (!directory.mkdirs()) {
            return false;
        }/* w  ww  .j  av  a 2 s .  c o  m*/
    }
    File f = new File(directoryName, ".probe");
    try {
        if (f.exists()) {
            f.delete();
        }
        if (!f.createNewFile()) {
            return false;
        }
        f.delete();
        return true;
    } catch (IOException ex) {
        return false;
    }
}

From source file:Main.java

private static void clearDir(String projectDir) {
    File file = new File(projectDir);
    File[] files = file.listFiles();
    if (files == null || files.length == 0) {
        return;/*from  www.j  av  a2 s .  c o m*/
    }
    for (File f : files) {
        if (f.isDirectory()) {
            clearDir(f.getAbsolutePath());
        } else {
            f.delete();
        }
    }
}

From source file:Main.java

/**
 * Copys a document file from one directory to another
 * @param documentPath//from ww  w . j ava2  s  . co m
 * @param destPath
 * @param filename
 * @param entityId src file Entity ID
 * @param newEntityId dest file Entity ID
 * @throws IOException
 */
public static void copyDocumentFile(File documentDir, File destDir, String filename, Object entityId,
        Object newEntityId) throws IOException {
    File src = new File(documentDir, filename);
    File dest = new File(destDir, filename);
    if (dest.exists())
        dest.delete();

    byte[] buffer = new byte[4096];
    int read = 0;
    InputStream in = null;
    OutputStream out = null;
    try {
        in = new BufferedInputStream(new FileInputStream(src));
        out = new BufferedOutputStream(new FileOutputStream(dest));
        while (true) {
            read = in.read(buffer);
            if (read == -1) {
                // -1 bedeutet EOF
                break;
            }
            out.write(buffer, 0, read);
        }
    } finally {
        if (in != null) {
            try {
                in.close();
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        }
    }
}

From source file:Main.java

private static boolean checkFsWritable(String dir) {

    if (dir == null)
        return false;

    File directory = new File(dir);

    if (!directory.isDirectory()) {
        if (!directory.mkdirs()) {
            return false;
        }//w w  w .  j  a v  a2s .c o  m
    }

    File f = new File(directory, ".keysharetestgzc");
    try {
        if (f.exists()) {
            f.delete();
        }
        if (!f.createNewFile()) {
            return false;
        }
        f.delete();
        return true;

    } catch (Exception e) {
    }
    return false;

}

From source file:Main.java

/** Delete files or folders */
public static boolean deleteFiles(final String path) {
    boolean ret = true;
    File f = new File(path);
    if (!f.exists())
        return true;
    if (!f.isDirectory()) {
        ret = f.delete() == false || ret == false ? false : true;
    } else {//from   www  .j a  va2s  . c  om
        File[] files = f.listFiles();
        if (files != null) {
            // Get folders list
            for (int i = 0; i < files.length; i++) {
                ret = deleteFiles(files[i].getPath()) == false || ret == false ? false : true;
                if (ret == false)
                    return false;
            }
        }
        ret = f.delete() == false || ret == false ? false : true;
    }
    return ret;
}

From source file:com.gistlabs.mechanize.util.Util.java

/** Returns a new File object from the given fileName and deleting the file if already exists. */
public static File newFile(String fileName) {
    File file = new File(fileName);
    if (file.exists())
        file.delete();
    return file;//  w w w .j  av a  2s. c o  m
}

From source file:Main.java

/**
 * Copy all the files from a folder to another.
 * @param srcFolder Source folder.//from  w  ww . j a v  a2s. c  om
 * @param dstFolder Destination folder.
 * @param cleanDstFolder Tell if destination folder has to be cleared before the copy starts.
 * @return {@code true} if all the files where copied, {@code false} otherwise.
 */
public static boolean copyFiles(File srcFolder, File dstFolder, boolean cleanDstFolder) {
    boolean retVal = true;
    File[] files;

    //Clean destination folder
    if (cleanDstFolder) {
        files = dstFolder.listFiles();
        if (files != null) {
            for (File file : files) {
                file.delete();
            }
        }
    }

    //Copy files
    files = srcFolder.listFiles();
    if (files != null) {
        for (File file : files) {
            boolean copied = copyFile(file, new File(dstFolder, file.getName()));
            retVal = retVal && copied;
        }
    }

    return retVal;
}

From source file:Main.java

private static File getBackupFileName(File f, boolean isGoodBackup) {
    File f2 = null;//from ww  w .ja  v a 2  s  . c  o m
    String prefix = f.getAbsolutePath() + (isGoodBackup ? ".good.bak." : ".corrupted.bak.");
    for (int i = MAX_BACKUP_FILES - 1; i > 2; i--) {
        File to = new File(prefix + i);
        File from = new File(prefix + (i - 1));
        if (to.exists())
            to.delete();
        if (from.exists()) {
            if (!from.renameTo(to))
                Log.e("cr3", "Cannot rename DB file " + from + " to " + to);
        }
    }
    f2 = new File(prefix + 2);
    if (f2.exists())
        if (!f2.delete())
            Log.e("cr3", "Cannot remove DB file " + f2);
    return f2;
}

From source file:Main.java

public static boolean saveFile(String filePath, InputStream inputStream) throws IOException {
    boolean result = false;
    if (filePath != null && inputStream != null) {
        Log.d(TAG, "filePath:" + filePath);
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        }/*  ww w.  j ava 2  s.c  om*/
        if (file.createNewFile()) {
            FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());
            byte[] buf = new byte[1024];
            int size = 0;
            while ((size = inputStream.read(buf, 0, 1024)) != -1) {
                fos.write(buf, 0, size);
            }

            fos.flush();
            fos.close();
            inputStream.close();
            result = true;
        }
    }
    return result;
}

From source file:com.joshlong.esb.springintegration.modules.net.sftp.Main.java

static void run(SFTPSessionFactory sftpSessionFactory, String lp, String rp) throws Throwable {
    // local path
    File local = new File(lp); // obviously this is just for test. Do what you need to do in your own

    // we are testing, after all
    if (local.exists() && (local.list().length > 0)) {
        for (File f : local.listFiles()) {
            if (!f.delete()) {
                logger.debug("couldn't delete " + f.getAbsolutePath());
            }//from  w  w  w.jav  a 2 s.com
        }
    }

    Resource localDirectory = new FileSystemResource(local);

    // pool
    QueuedSFTPSessionPool queuedSFTPSessionPool = new QueuedSFTPSessionPool(sftpSessionFactory);
    queuedSFTPSessionPool.afterPropertiesSet();

    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.setPoolSize(10);
    taskScheduler.setErrorHandler(new ErrorHandler() {
        public void handleError(Throwable t) {
            logger.debug("error! ", t);
        }
    });

    taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
    taskScheduler.initialize();

    // synchronizer
    final SFTPInboundSynchronizer sftpInboundSynchronizer = new SFTPInboundSynchronizer();
    sftpInboundSynchronizer.setLocalDirectory(localDirectory);
    sftpInboundSynchronizer.setRemotePath(rp);
    sftpInboundSynchronizer.setAutoCreatePath(true);
    sftpInboundSynchronizer.setPool(queuedSFTPSessionPool);
    sftpInboundSynchronizer.setShouldDeleteDownloadedRemoteFiles(false);
    sftpInboundSynchronizer.setTaskScheduler(taskScheduler);
    sftpInboundSynchronizer.afterPropertiesSet();
    sftpInboundSynchronizer.start();

    /*
        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(60 * 1000); // 1 minute
            
                    sftpInboundSynchronizer.stop();
            
                } catch (InterruptedException e) {
                    // don't care
                }
            }
        }).start();
    */
}