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 File rename(File file, String newName) {
    File newFile = new File(file.getParent(), newName);
    if (!newFile.equals(file)) {
        if (newFile.exists()) {
            newFile.delete();
        }//from w w w  .ja  va  2 s . com
        file.renameTo(newFile);
    }
    return newFile;
}

From source file:Main.java

public static void deleteDirContent(File dir) {
    final File[] files = dir.listFiles();
    if (files != null) {
        for (final File file : files) {
            if (file.isDirectory()) {
                deleteDirContent(file);//from  w  ww  .  ja  v  a  2s.  com
            }
            file.delete();
        }
    }
}

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.
 *//*from ww  w . j a  v a 2  s .c  o  m*/
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:com.springsource.open.db.BaseDatasourceTests.java

@BeforeClass
@AfterClass/*  w w  w  . j a v a  2s  .co  m*/
public static void clearLog() {
    // Ensure that Atomikos logging directory exists
    File dir = new File("atomikos");
    if (!dir.exists()) {
        dir.mkdir();
    }
    // ...and delete any stale locks (this would be a sign of a crash)
    File tmlog = new File("atomikos/tmlog.lck");
    if (tmlog.exists()) {
        tmlog.delete();
    }
}

From source file:at.medevit.elexis.gdt.handler.GDTFileInputHandler.java

private static void delete(File file) {
    try {/*from  w  ww  .j a  v a  2 s  .  co m*/
        boolean deleted = file.delete();
        if (deleted) {
            logger.log("Deleted " + file.getAbsolutePath(), Log.DEBUGMSG);
        } else {
            logger.log("Error deleting " + file.getAbsolutePath(), Log.WARNINGS);
        }
    } catch (SecurityException e) {
        logger.log(e, "Error deleting " + file.getAbsolutePath(), Log.WARNINGS);
    }
}

From source file:com.abid_mujtaba.bitcoin.tracker.data.Data.java

public static boolean clear() // Method for clearing the data by deleting the file
{
    File file = data_file();

    boolean success = file.delete();

    if (success) {
        data_file();//from  w  ww  .j  av a 2  s .  co  m
    } // We call data_file again to create an empty file once the original file has been deleted.

    return success;
}

From source file:Main.java

public static void imgCacheWrite(Context context, String cacheImgFileName, String imgBase64Str) {
    File cacheImgFile = new File(context.getFilesDir() + "/" + cacheImgFileName);
    if (cacheImgFile.exists()) {
        cacheImgFile.delete();
    }/*from   w  ww .  j  a va  2 s  .c o m*/

    FileOutputStream fos;
    try {
        fos = context.openFileOutput(cacheImgFileName, Context.MODE_PRIVATE);
        fos.write(imgBase64Str.getBytes("utf-8"));
        fos.flush();
        fos.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:Main.java

public static boolean saveBeanToFile(String filePath, Object bean) {
    File file = new File(filePath);
    ObjectOutputStream outputStream = null;
    if (file.exists()) {
        file.delete();
    }//from w w  w. java 2 s  .c  om

    try {
        outputStream = new ObjectOutputStream(new FileOutputStream(file));
        outputStream.writeObject(bean);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeQuiltly(outputStream);
    }

    return true;
}

From source file:Main.java

public static File getFileForCrypt(String hash, Context context) {
    File externalCacheDir = new File(
            new File(new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data"),
                    context.getPackageName()),
            "cypher");
    externalCacheDir.mkdirs();/*from w  w w . j  a  v  a  2  s.  com*/

    // clear old items
    String[] files = externalCacheDir.list();
    long th = new Date().getTime() - 24 * 60 * 60 * 1000; // expire one day
    for (int i = 0; i < files.length; i++) {
        File tmp = new File(externalCacheDir, files[i]);
        if (tmp.lastModified() < th) {
            tmp.delete();
        }
    }

    // add new file
    File dst = new File(externalCacheDir, hash + ".tmp");
    if (dst.exists()) {
        dst.delete();
    }
    //      dst.deleteOnExit();
    return dst;
}

From source file:org.pau.assetmanager.viewmodel.chart.ResourceImageGenerator.java

private static void cleanTempFolder(File directory) {
    Calendar cal = Calendar.getInstance();
    int daysBack = 1;
    cal.add(Calendar.DAY_OF_MONTH, daysBack * -1);
    long purgeTime = cal.getTimeInMillis();
    File[] files = directory.listFiles();
    for (File file : files) {
        if (file.lastModified() < purgeTime) {
            file.delete();
        }//ww w.  j a v a  2  s.co m
    }
}