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 boolean delServerConfig(Context ctx) {
    if (ctx == null) {
        throw new IllegalArgumentException("Context can't null.");
    }//from  w ww  .  jav  a2s  .c o  m
    String sdkServerFolder = getSDKServerFolder(ctx);
    File file = new File(sdkServerFolder, SDK_SERVER_NAME);
    if (file.exists()) {
        file.delete();
    }
    return true;
}

From source file:Main.java

/**
 * Saves a Bitmap object to disk for analysis.
 *
 * @param bitmap The bitmap to save./*from  w w  w .  j  av a2  s.  c  o  m*/
 */
public static void saveBitmap(final Bitmap bitmap) {
    final String root = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
            + "tensorflow";
    //LOGGER.i("Saving %dx%d bitmap to %s.", bitmap.getWidth(), bitmap.getHeight(), root);
    final File myDir = new File(root);

    if (!myDir.mkdirs()) {
        //LOGGER.i("Make dir failed");
    }

    final String fname = "preview.png";
    final File file = new File(myDir, fname);
    if (file.exists()) {
        file.delete();
    }
    try {
        final FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 99, out);
        out.flush();
        out.close();
    } catch (final Exception e) {
        //LOGGER.e(e, "Exception!");
    }
}

From source file:Utils.java

public static void deleteRecursive(File dir) throws IOException {
    if (dir.isDirectory())
        deleteContentsRecursive(dir);/*ww  w.j  av a2s .  co  m*/
    if (!dir.delete())
        throw new IOException("Unable to delete " + dir);
}

From source file:Main.java

public static boolean delDir(String dirPath, boolean delFile) {
    if (delFile) {
        File file = new File(dirPath);
        if (file.isFile()) {
            return file.delete();
        } else if (file.isDirectory()) {
            if (file.listFiles().length == 0) {
                return file.delete();
            } else {
                int zfiles = file.listFiles().length;
                File[] delfile = file.listFiles();
                for (int i = 0; i < zfiles; i++) {
                    if (delfile[i].isDirectory()) {
                        delDir(delfile[i].getAbsolutePath(), true);
                    }/*  w w  w . j a va 2  s .  c  o  m*/
                    delfile[i].delete();
                }
                return file.delete();
            }
        } else {
            return false;
        }
    } else {
        return new File(dirPath).delete();
    }
}

From source file:Main.java

/**
 * Copy file from oldPath to newPath/*from   ww w.ja  va2 s. c  o m*/
 *
 * @param oldPath
 * @param newPath
 */
public static void copyFile(String oldPath, String newPath) {
    try {
        int byteread = 0;
        File oldfile = new File(oldPath);
        File newfile = new File(newPath);
        if (newfile.exists()) {
            newfile.delete();
        }
        newfile.createNewFile();
        if (oldfile.exists()) {
            FileInputStream inStream = new FileInputStream(oldPath);
            FileOutputStream outStream = new FileOutputStream(newPath);
            byte[] buffer = new byte[1024];
            while ((byteread = inStream.read(buffer)) > 0) {
                outStream.write(buffer, 0, byteread);
            }
            inStream.close();
            outStream.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

private static void deleteDirectorySync(File dir) {
    try {/*from w  ww. ja  va 2s  .com*/
        File[] files = dir.listFiles();
        if (files != null) {
            for (File file : files) {
                String fileName = file.getName();
                if (!file.delete()) {
                    Log.e(TAG, "Failed to remove " + file.getAbsolutePath());
                }
            }
        }
        if (!dir.delete()) {
            Log.w(TAG, "Failed to remove " + dir.getAbsolutePath());
        }
        return;
    } catch (Exception e) {
        Log.e(TAG, "Failed to remove old libs, ", e);
    }
}

From source file:Main.java

public static void deleteAndReport(File file) {
    if (file != null && file.exists()) {
        // remove garbage
        if (!file.delete()) {
            Log.w(t, file.getAbsolutePath() + " will be deleted upon exit.");
            file.deleteOnExit();/*from  w w  w  .  j a  v a2s . c o  m*/
        } else {
            Log.w(t, file.getAbsolutePath() + " has been deleted.");
        }
    }
}

From source file:Main.java

public static boolean writeToSdcard(byte[] data, String path, String fileName) {
    FileOutputStream fos = null;/*from  w w w.  j av  a 2  s  .co  m*/
    try {
        File filePath = new File(path);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        File file = new File(path + fileName);
        if (file.exists()) {
            file.delete();
        }
        fos = new FileOutputStream(file);
        fos.write(data);
        fos.flush();
        return true;
    } catch (Exception e) {
        return false;
    } finally {
        try {
            if (fos != null)
                fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void saveBitmap(Bitmap bm, String picName) {
    try {// w  w  w. j av a 2 s. c om
        if (!isFileExist("")) {
            createSDDir("");
        }
        File f = new File(SDPATH, picName);
        if (f.exists()) {
            f.delete();
        }
        FileOutputStream out = new FileOutputStream(f);
        bm.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 File createTempDir() throws IOException {
    File temp;

    temp = File.createTempFile("encfs-java-tmp", Long.toString(System.nanoTime()));
    if (!temp.delete()) {
        throw new IOException("Could not delete temporary file " + temp.getAbsolutePath());
    }//from   w w  w  . j av  a 2s . com

    if (!temp.mkdir()) {
        throw new IOException("Could not create temporary directory");
    }

    return temp;
}