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

private static void deleteOrThrow(File file) throws IOException {
    if (file.exists()) {
        boolean isDeleted = file.delete();
        if (!isDeleted) {
            throw new IOException(String.format("File %s can't be deleted", file.getAbsolutePath()));
        }//from  w  ww.j a  v  a  2 s .c o  m
    }
}

From source file:Main.java

public static void write(InputStream in, File file) {
    if (file.exists()) {
        file.delete();
    }/*from w ww. j  a va  2 s.  c o  m*/
    try {
        file.createNewFile();
        FileOutputStream out = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        while (in.read(buffer) > -1) {
            out.write(buffer);
        }
        out.flush();
        in.close();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean deleteAll(File dir) {
    String fileNames[] = dir.list();
    if (fileNames == null)
        return false;
    for (int i = 0; i < fileNames.length; i++) {
        File file = new File(dir, fileNames[i]);
        if (file.isFile())
            file.delete();
        else if (file.isDirectory())
            deleteAll(file);//from ww w .  j a va  2  s  .  c  om
    }

    return dir.delete();
}

From source file:Main.java

public static void saveBitmapToCacheDir(Context context, Bitmap bitmap, String name) {
    try {/*  w  w w .j a v a  2  s .  co  m*/
        File file = new File(context.getCacheDir(), name);
        if (file.exists()) {
            file.delete();
        }
        FileOutputStream fos = context.openFileOutput(name, MODE_PRIVATE);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.cj.restspecs.mojo.Util.java

public static void delete(File path) {
    if (!path.delete())
        throw new RuntimeException("Could not delete " + path.getAbsolutePath());
}

From source file:Main.java

public static boolean canWriteTo(File dir) {
    try {/*from  w w w .j  av a 2s . c o m*/
        File tmp = new File(dir, "del.me");
        tmp.createNewFile();
        tmp.deleteOnExit();
        tmp.delete();
        return true;
    } catch (Exception ex) {
        return false;
    }

}

From source file:Main.java

public static void removeOldVersionFiles(Context context, String fileName) {
    File dataDirPath = new File(context.getApplicationInfo().dataDir);
    File[] fileList = dataDirPath.listFiles();

    for (File file : fileList) {
        if (file.getAbsoluteFile().toString().endsWith(fileName)) {
            file.delete();
        }/*w  ww. j a va 2  s .  c  o  m*/
    }
}

From source file:jetbrick.tools.chm.Application.java

private static void clean() throws IOException {
    File f = new File(Config.apiLocation, "htmlhelp.hhp");
    if (f.exists())
        f.delete();
    f = new File(Config.apiLocation, "htmlhelp.hhc");
    if (f.exists())
        f.delete();/*  ww  w  .j ava  2  s  .co m*/
    f = new File(Config.apiLocation, "htmlhelp.hhk");
    if (f.exists())
        f.delete();
    f = new File(Config.apiLocation, "build.bat");
    if (f.exists())
        f.delete();
    f = new File(Config.apiLocation, "hhc.exe");
    if (f.exists())
        f.delete();
    f = new File(Config.apiLocation, "hha.dll");
    if (f.exists())
        f.delete();
    f = new File(Config.apiLocation, "redirs/");
    if (f.exists())
        FileUtils.deleteDirectory(f);
}

From source file:com.linkedin.pinot.tools.admin.command.StartZookeeperCommand.java

public static File createAutoDeleteTempDir() {
    File tempdir = Files.createTempDir();
    tempdir.delete();
    tempdir.mkdir();/*from w  ww. ja  va 2s .c  o  m*/

    tempdir.deleteOnExit();
    return tempdir;
}

From source file:Main.java

public static void deleteFile(File file) {
    try {//  www.  ja  v  a 2s. c o  m
        if (file != null && file.isFile() && file.exists()) {
            file.delete();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}