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:ch.elexis.core.data.util.FileUtility.java

/**
 * Lscht Datei//  w  ww .  j ava  2 s  .  c  om
 */
public static void deleteFile(final String filePathName) throws IllegalArgumentException {
    if (doesFileExist(filePathName)) {
        File file = new File(filePathName);
        file.delete();
    }
}

From source file:Main.java

/**
 * Moves a file to another location./*from   w  w w . j a v  a 2  s.  c o  m*/
 */
public static void move(File from, File to) {
    if (!from.renameTo(to)) {
        copyFile(from, to);
        if (!from.delete()) {
            if (!to.delete()) {
                throw new RuntimeException("Unable to delete " + to);
            }
            throw new RuntimeException("Unable to delete " + from);
        }
    }
}

From source file:Main.java

public static boolean cacheDrawable(String packageName, int resId, BitmapDrawable drawable) {
    try {/*ww w .jav a  2  s  . co m*/
        File f = new File("/data/data/" + PACKAGE_NAME + "/cache/icons/", packageName + "_" + resId);
        if (f.exists()) {
            f.delete();
        }
        f.createNewFile();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        drawable.getBitmap().compress(CompressFormat.PNG, 0, bos);
        new FileOutputStream(f).write(bos.toByteArray());
        f.setReadable(true, false);
        f.setWritable(true, false);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Utils.java

/**
 * Empty and delete a folder (and subfolders).
 * @param folder/*  w  w w . j  a  v  a2s  .com*/
 *            folder to empty
 */
public static void rmdir(final File folder) {
    // check if folder file is a real folder
    if (folder.isDirectory()) {
        File[] list = folder.listFiles();
        if (list != null) {
            for (int i = 0; i < list.length; i++) {
                File tmpF = list[i];
                if (tmpF.isDirectory()) {
                    rmdir(tmpF);
                }
                tmpF.delete();
            }
        }
        if (!folder.delete()) {
            System.out.println("can't delete folder : " + folder);
        }
    }
}

From source file:Main.java

public static void deleteDir(File file) {
    if (file.isDirectory()) {
        for (File _file : file.listFiles()) {
            if (_file.isDirectory()) {
                deleteDir(_file);//from  ww w  .ja  v a 2 s.c  o m
            } else {
                _file.delete();
            }
        }
    } else {
        file.delete();
    }
}

From source file:Main.java

public static void writeFile(InputStream in, File file) throws IOException {
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();//www  .  j av  a 2s . co m
    } else {
        file.delete();
    }

    FileOutputStream out = new FileOutputStream(file);
    byte[] buffer = new byte[1024 * 128];
    int len = -1;
    while ((len = in.read(buffer)) != -1) {
        out.write(buffer, 0, len);
    }
    out.flush();
    out.close();
    in.close();
}

From source file:com.collaide.fileuploader.helper.TestHelper.java

private static void clean() {
    File index = new File(getTestCopyDir());
    String[] entries = index.list();
    for (String s : entries) {
        File currentFile = new File(index.getPath(), s);
        currentFile.delete();
    }/*from  w ww . j  a  v a2  s. c  om*/
}

From source file:Main.java

/**
 * Copy data from a source stream to destFile. Return true if succeed,
 * return false if failed./*from w ww.j a  va  2  s  .  c o  m*/
 */
public static boolean copyToFile(InputStream inputStream, File destFile) {
    try {
        if (destFile.exists()) {
            destFile.delete();
        }
        FileOutputStream out = new FileOutputStream(destFile);
        try {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) >= 0) {
                out.write(buffer, 0, bytesRead);
            }
        } finally {
            out.flush();
            try {
                out.getFD().sync();
            } catch (IOException e) {
            }
            out.close();
        }
        return true;
    } catch (IOException e) {
        return false;
    }
}

From source file:Main.java

public static String catchStreamToFile(String catchFile, InputStream inStream) throws Exception {

    File tempFile = new File(catchFile);
    try {/*from ww  w.j a  va 2s .c  om*/
        if (tempFile.exists()) {
            tempFile.delete();
        }
        tempFile.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = inStream.read(buffer)) != -1) {
        fileOutputStream.write(buffer, 0, len);
    }
    inStream.close();
    fileOutputStream.close();
    return catchFile;
}

From source file:Main.java

public static void cleanSharedPreference(Context context, String sharedPreferenceName) {
    File file = new File("/data/data/" + context.getPackageName().toString() + "/shared_prefs",
            sharedPreferenceName);/*from  w  w w  . j  a va2s  .  c  o m*/
    if (file.exists()) {
        file.delete();
    }
}