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:Utils.java

private static void deleteContentsRecursive(File file) throws IOException {
    File[] files = file.listFiles();
    for (File child : files) {
        if (child.isDirectory())
            deleteContentsRecursive(child);
        if (!child.delete())
            throw new IOException("Unable to delete " + child.getPath());
    }//from  w w w .ja va 2  s  .c o  m
}

From source file:Main.java

public static boolean delete(File file) {
    file.setWritable(true);// ww w . j av a2 s . c o  m
    try {
        if (!file.delete()) {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(0);
            fos.flush();
            fos.close();
        }
        Log.d("delete", "Deleted file: " + file + " successfully");
        return true;
    } catch (IOException e) {
        Log.d("delete", "The deleting file: " + file + " is not successfully", e);
        return false;
    }
}

From source file:Main.java

public static void writeFile(String filename, String text) throws Exception {
    File f = new File(filename);
    if (!f.exists())
        f.createNewFile();/* www .j  av  a  2s. co m*/
    else {
        f.delete();
        f.createNewFile();
    }
    BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "utf-8"));

    // new BufferedWriter(new FileWriter(filename,
    // false),"utf-8");
    output.write(text);

    output.flush();
    output.close();
}

From source file:Main.java

public static void saveBitmap(Bitmap bm, String picName) {
    try {//from   w  w w .  j  a  v a2s  . c  o  m
        if (!isFileExist("")) {
            File tempf = createSDDir("");
        }
        File f = new File(SDPATH, picName + ".JPEG");
        if (f.exists()) {
            f.delete();
        }
        FileOutputStream out = new FileOutputStream(f);
        bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static final void deleteOrThrow(@NonNull File file) throws IOException {
    if (!file.exists()) {
        return;//from   w w  w.ja  v a  2 s.c o  m
    }

    boolean result = file.delete();
    if (!result) {
        throw new IOException("Failed to delete a file, file=" + file.getAbsolutePath());
    }
}

From source file:Main.java

public static File bitmap2File(Bitmap bitmap, String filename) {
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    OutputStream outStream = null;

    File file = new File(extStorageDirectory, filename + ".png");
    if (file.exists()) {
        file.delete();
        file = new File(extStorageDirectory, filename + ".png");
        Log.e("file exist", "" + file + ",Bitmap= " + filename);
    }/*from  w  w w  . j av a2 s  . c om*/
    try {
        outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.e("file", "" + file);
    return file;

}

From source file:keepinchecker.setup.KeepInCheckerTestCase.java

@AfterClass
public static void cleanUp() {
    File database = new File(StringUtils.substringAfterLast(Constants.DATABASE_PATH, ":"));
    database.delete();
}

From source file:Main.java

/**
 * Recursively deletes folder and files.
 * //from  w  ww . j a  va2 s  . c  om
 * @param folder
 */
private static void delete(File folder) {
    File[] files = folder.listFiles();
    for (int i = 0; i < files.length; i++) {
        File file = files[i];
        if (file.isDirectory()) {
            delete(file);
        }
        file.delete();
    }
    folder.delete();
}

From source file:IO.Files.java

/**
 * Returns true if the given file is successfully deleted
 *
 * @param filePath file's path to delete
 * @return true if the given file is successfully deleted
 *///from w  ww.j  ava  2s .c om
public static boolean DeleteFile(String filePath) {
    File file = new File(filePath);
    return file.delete();
}

From source file:Main.java

private static void cleanDir(File dir) {

    File[] files = dir.listFiles();

    for (File file : files) {
        if (file.lastModified() + 1000 < System.currentTimeMillis()) { //more than a day old
            file.delete();
        }//www  .ja  v  a  2  s .co m
    }
}