List of usage examples for java.io File delete
public boolean delete()
From source file:Main.java
public static boolean deleteFile(String path) { if (TextUtils.isEmpty(path)) { return true; }//from www .j av a 2 s . c o m File file = new File(path); if (file.exists()) { if (file.isFile()) { return file.delete(); } else if (file.isDirectory()) { for (File f : file.listFiles()) { if (f.isFile()) { f.delete(); } else if (f.isDirectory()) { deleteFile(f.getAbsolutePath()); } } return file.delete(); } return false; } return true; }
From source file:Main.java
/** * Deletes the contents of {@code dir}. Throws an IOException if any file * could not be deleted, or if {@code dir} is not a readable directory. *///w w w .jav a 2 s. c o m public static void deleteContents(File dir) throws IOException { File[] files = dir.listFiles(); if (files == null) { throw new IOException("not a readable directory: " + dir); } for (File file : files) { if (file.isDirectory()) { deleteContents(file); } if (!file.delete()) { throw new IOException("failed to delete file: " + file); } } }
From source file:Main.java
public static boolean deleteFile(String path) { // If path == null will throw NullPointException. if (TextUtils.isEmpty(path)) { return false; }//from w w w . ja v a 2s. c om File file = new File(path); if (!file.exists()) { return false; } boolean flag = file.delete(); File parent = file.getParentFile(); if (isEmptyDirectory(parent)) { parent.delete(); } return flag; }
From source file:Main.java
/** * Deletes an image given its Uri./*from ww w.j a v a2 s.co m*/ * * @param cameraPicUri * @param context * @return true if it was deleted successfully, false otherwise. */ public static boolean deleteImageWithUriIfExists(Uri cameraPicUri, Context context) { if (cameraPicUri != null) { File fdelete = new File(cameraPicUri.getPath()); if (fdelete.exists()) { if (fdelete.delete()) { context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); return true; } } } return false; }
From source file:Main.java
public static File setOutPutImage(String name) { File outputimage = new File(Environment.getExternalStorageDirectory(), name + ".jpg"); if (outputimage.exists()) { outputimage.delete(); }//from ww w . j av a 2 s . c o m try { outputimage.createNewFile(); } catch (IOException e) { e.printStackTrace(); } return outputimage; }
From source file:Main.java
public static void deleteDir(File target) { if (target.exists() == false) { return;/*from ww w. ja va2s. c o m*/ } if (target.isFile()) { target.delete(); } if (target.isDirectory()) { File[] files = target.listFiles(); for (int i = 0; i < files.length; i++) { deleteDir(files[i]); } target.delete(); } }
From source file:Main.java
public static boolean saveFile(String filePath, String str) throws IOException { boolean result = false; if (filePath != null && str != null) { Log.d(TAG, "filePath:" + filePath); File file = new File(filePath); if (file.exists()) { file.delete(); }//from w w w . j av a 2s.c o m if (file.createNewFile()) { FileOutputStream fos = new FileOutputStream(file.getAbsolutePath()); fos.write(str.getBytes("gb18030")); fos.flush(); fos.close(); result = true; } } return result; }
From source file:Main.java
public static String saveImg(Bitmap b, String name) throws Exception { String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "QuCai/shareImg/"; File mediaFile = new File(path + File.separator + name + ".jpg"); if (mediaFile.exists()) { mediaFile.delete(); }/*from w w w . j a va 2 s. c o m*/ if (!new File(path).exists()) { new File(path).mkdirs(); } mediaFile.createNewFile(); FileOutputStream fos = new FileOutputStream(mediaFile); b.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); b.recycle(); b = null; System.gc(); return mediaFile.getPath(); }
From source file:Main.java
public static File createTempFile(String prefix, String suffix, File dir, boolean isReCreat) throws IOException { int exceptionsCount = 0; while (true) { try {/*from w ww .j ava2 s . c om*/ File file = File.createTempFile(prefix, suffix, dir).getCanonicalFile(); if (isReCreat) { file.delete(); file.createNewFile(); } return file; } catch (IOException ioex) { // fixes java.io.WinNTFileSystem.createFileExclusively access denied if (++exceptionsCount >= 50) { throw ioex; } } } }
From source file:Main.java
/** * save the bitmap to local,add give a white bg color * @param bitmap/* w ww . ja v a2s . c om*/ * @param path * @return */ public static boolean saveBitmapNoBgToSdCard(Bitmap bitmap, String path) { BufferedOutputStream bos = null; try { File file = new File(path); if (file.exists()) file.delete(); bos = new BufferedOutputStream(new FileOutputStream(file)); int w = bitmap.getWidth(); int h = bitmap.getHeight(); int w_new = w; int h_new = h; Bitmap resultBitmap = Bitmap.createBitmap(w_new, h_new, Bitmap.Config.ARGB_8888); // Paint paint = new Paint(); // paint.setColor(Color.WHITE); Canvas canvas = new Canvas(resultBitmap); canvas.drawColor(Color.WHITE); canvas.drawBitmap(bitmap, new Rect(0, 0, w, h), new Rect(0, 0, w_new, h_new), null); resultBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); bos.flush(); resultBitmap.recycle(); return true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; }