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 deleteSinglePicture(Context ctx, String path) {
    String params[] = new String[] { path };
    ctx.getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            MediaStore.Images.Media.DATA + " LIKE ?", params);
    File file = new File(path);
    File pFile = file.getParentFile();
    if (pFile != null && pFile.isDirectory()) {
        File files[] = pFile.listFiles();
        if (files == null || files.length == 0) {
            pFile.delete();
        }//from   ww w  . j  a va 2 s  . c o m
    }
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromUrl(String urlpath, long allowedBmpMaxMemorySize) {

    String tmpDir = System.getProperty("java.io.tmpdir", ".");
    File saveFile = new File(tmpDir, UUID.randomUUID().toString());
    if (saveFile.exists()) {
        saveFile.delete();
    }/*from  w  ww  . j a  v a 2 s  .  c om*/
    try {
        saveFile.createNewFile();
        File ret = downloadFile(urlpath, saveFile);
        if (ret == null) {// fail.
            return null;
        }
        Bitmap bmp = decodeSampledBitmapFromPath(saveFile.getAbsolutePath(), allowedBmpMaxMemorySize);
        return bmp;
    } catch (Throwable err) {
        err.printStackTrace();

    } finally {
        if (saveFile.exists()) {
            saveFile.delete();// true.
        }
    }
    return null;
}

From source file:Main.java

public static boolean saveBitmap(Bitmap bmp, String path) {
    File file = new File(path);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();/*from w  w w  . j av a  2 s . c o  m*/
    }
    if (file.exists()) {
        file.delete();
    }
    try {

        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.JPEG, 80, out);
        out.flush();
        out.close();
        Log.i("saveBitmap success:", path);
        return true;
    } catch (Exception e) {
        Log.e("saveBitmap:" + path, e.toString());
    }
    return false;

}

From source file:Main.java

/**
 * Save PNG image with background color/*from  www.  j a v  a2 s  .co m*/
 * @param strFileName Save file path
 * @param bitmap Input bitmap
 * @param nBackgroundColor background color
 * @return whether success or not
 */
public static boolean saveBitmapPNGWithBackgroundColor(String strFileName, Bitmap bitmap,
        int nBackgroundColor) {
    boolean bSuccess1 = false;
    boolean bSuccess2 = false;
    boolean bSuccess3;
    File saveFile = new File(strFileName);

    if (saveFile.exists()) {
        if (!saveFile.delete())
            return false;
    }

    int nA = (nBackgroundColor >> 24) & 0xff;

    // If Background color alpha is 0, Background color substitutes as white
    if (nA == 0)
        nBackgroundColor = 0xFFFFFFFF;

    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    canvas.drawColor(nBackgroundColor);
    canvas.drawBitmap(bitmap, rect, rect, new Paint());

    OutputStream out = null;

    try {
        bSuccess1 = saveFile.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        out = new FileOutputStream(saveFile);
        bSuccess2 = newBitmap.compress(CompressFormat.PNG, 100, out);
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        if (out != null) {
            out.flush();
            out.close();
            bSuccess3 = true;
        } else
            bSuccess3 = false;

    } catch (IOException e) {
        e.printStackTrace();
        bSuccess3 = false;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    return (bSuccess1 && bSuccess2 && bSuccess3);
}

From source file:Main.java

private static void deleteDirectoryContent(String directoryPath) {
    File dir = new File(directoryPath);
    if (dir.exists() && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            File file = new File(dir, children[i]);
            if (file.isDirectory()) {
                deleteDirectoryContent(file.getAbsolutePath());
            } else {
                file.delete();
            }/*from  w  w  w  .j  a va 2  s  .  c o  m*/
        }
        dir.delete();
    }
}

From source file:com.openkm.applet.Util.java

/**
 * Creates a temporal and unique directory
 * //  ww w  .j  ava2 s.  c om
 * @throws IOException If something fails.
 */
public static File createTempDir() throws IOException {
    File tmpFile = File.createTempFile("okm", null);

    if (!tmpFile.delete())
        throw new IOException();
    if (!tmpFile.mkdir())
        throw new IOException();

    return tmpFile;
}

From source file:net.adamcin.oakpal.testing.TestPackageUtil.java

public static File prepareTestPackageFromFolder(final String filename, final File srcFolder)
        throws IOException {
    if (srcFolder == null || !srcFolder.isDirectory()) {
        throw new IOException("expected directory in srcFolder parameter for test package filename "
                + String.valueOf(filename));
    }// w ww .  j  ava2s. c  o  m
    File file = new File(testPackagesRoot, filename);
    if (file.exists()) {
        file.delete();
    }

    try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(file))) {
        add(srcFolder, srcFolder, jos);
    }

    return file;
}

From source file:Main.java

public static boolean installDll(Context context) {
    boolean isSuccess = false;
    File file = context.getFileStreamPath("Disdll.dll");
    boolean isDeleteSuccess = true;
    if (file.exists()) {
        isDeleteSuccess = file.delete();
    }/*from  w w w.j ava  2 s  .  co m*/
    if (isDeleteSuccess) {
        try {
            FileOutputStream outputStream = context.openFileOutput("Disdll.dll", Context.MODE_PRIVATE);
            InputStream inputStream = context.getAssets().open("Disdll.dll");
            byte[] temp = new byte[1024];
            int len = -1;
            while ((len = inputStream.read(temp)) != -1) {
                outputStream.write(temp, 0, len);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        isSuccess = checkIfDllInstalled(context);
    }
    return isSuccess;
}

From source file:Main.java

public static boolean saveObjectToFile(String filePath, Object object) {
    if (!TextUtils.isEmpty(filePath)) {
        File cacheFile = null;

        try {/*  w w  w.j a  v  a 2  s  .  c om*/
            cacheFile = new File(filePath);
            if (cacheFile.exists()) {
                cacheFile.delete();
            }

            if (!cacheFile.getParentFile().exists()) {
                cacheFile.getParentFile().mkdirs();
            }

            cacheFile.createNewFile();
        } catch (Throwable var6) {
            var6.printStackTrace();
            cacheFile = null;
        }

        if (cacheFile != null) {
            try {
                FileOutputStream t = new FileOutputStream(cacheFile);
                GZIPOutputStream gzos = new GZIPOutputStream(t);
                ObjectOutputStream oos = new ObjectOutputStream(gzos);
                oos.writeObject(object);
                oos.flush();
                oos.close();
                return true;
            } catch (Throwable var7) {
                var7.printStackTrace();
            }
        }
    }

    return false;
}

From source file:com.codefollower.lealone.omid.TestUtils.java

public static void delete(File f) throws IOException {
    if (f.isDirectory()) {
        for (File c : f.listFiles())
            delete(c);//ww w  . j  a  v  a 2s. c  o  m
    }
    if (!f.delete())
        throw new FileNotFoundException("Failed to delete file: " + f);
}