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 void deleteFile(File file) {
    if (file.isDirectory()) {
        if (file.list().length == 0)
            file.delete();
        else {/*from w ww.j av a  2 s  . c  om*/
            File[] files = file.listFiles();
            for (File f : files)
                deleteFile(f);
            deleteFile(file);
        }
    } else
        file.delete();
}

From source file:com.adaptris.core.stubs.TempFileUtils.java

public static File createTrackedDir(String prefix, String suffix, File baseDir, Object tracker)
        throws IOException {
    File f = File.createTempFile(prefix, suffix, baseDir);
    f.delete();
    f.mkdirs();/*from  w w  w.  j a v  a2s .c  o m*/
    cleaner.track(f, tracker, FileDeleteStrategy.FORCE);
    return trackFile(f, tracker);
}

From source file:Main.java

public static void deleteFile(String fileName) {
    File f = new File(fileName);
    if (f.exists()) {
        if (f.isFile()) {
            f.delete();
        } else if (f.isDirectory()) {
            String[] filelist = f.list();
            for (int i = 0; i < filelist.length; i++) {
                File readfile = new File(fileName + filelist[i]);
                deleteFile(fileName + filelist[i]);
            }//from  w w  w  .  j a va2  s .c  o m
        }
    }
}

From source file:Main.java

private static void removeExistFile(String path) {
    if (path != null) {
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }/*from   w w w  . ja  v  a 2s. c o  m*/
    }
}

From source file:Main.java

public static boolean delFile(String filePath) {
    File file = new File(filePath);
    if (!file.exists()) {
        return true;
    }//from  w  w  w . j  a  va  2  s  .c  om

    return file.delete();
}

From source file:Main.java

public static void deleteEveryFile(File file) {
    if (file.exists()) {
        if (file.isFile()) {
            file.delete();
        } else if (file.isDirectory()) {
            File afile[] = file.listFiles();
            if (afile != null) {
                for (int i = 0; i < afile.length; i++)
                    deleteEveryFile(afile[i]);

                file.delete();//from   w w  w . j  a v a  2 s .co  m
            } else {
                file.delete();
            }
        }
    }

}

From source file:Main.java

public static void deleteFolder(File f) {
    File[] files = f.listFiles();
    for (File fi : files) {
        if (fi.isDirectory()) {
            deleteFolder(fi);//from w  w w.  j a  v a 2  s.c  o m
        }
        fi.delete();
    }
}

From source file:Main.java

public static void savePic2SD(Bitmap bitmap, String path, String folder) {

    boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    if (sdCardExist) {
        File fileDir = new File(folder);
        if (!fileDir.exists()) {
            fileDir.mkdir();//from   w w  w.  j  a v  a 2  s  .  c  o m
        }
    }

    File file = new File(path);
    if (file.exists()) {
        file.delete();
    }
    try {
        FileOutputStream out = new FileOutputStream(file);

        if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
            out.flush();
            out.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static boolean writeToExternalStoragePublic(String filename, String fileContent) {
    boolean saved = false;
    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
    if (isExternalStorageAvailable() && !isExternalStorageReadOnly()) {
        try {/*from   w  ww . java2 s .  c  o m*/
            boolean exists = (new File(path)).exists();
            if (!exists) {
                new File(path).mkdirs();
            }

            //Remote legacy file
            File file = new File(path + filename);
            file.delete();

            // Open output stream
            FileOutputStream fOut = new FileOutputStream(path + filename, true);
            fOut.write(fileContent.getBytes());
            // Close output stream
            fOut.flush();
            fOut.close();

            saved = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return saved;
}

From source file:Main.java

public static boolean restoreFromBackup(File f) {
    File backup = new File(f.getAbsolutePath() + ".good.bak.2");
    if (f.exists())
        f.delete();
    if (backup.exists()) {
        if (backup.renameTo(f))
            return true;
    }//from w w  w. ja v a2  s .  c om
    return false;
}