Java Delete File if Exist deleteIfExists(final File file)

Here you can find the source of deleteIfExists(final File file)

Description

delete If Exists

License

Open Source License

Declaration

private static boolean deleteIfExists(final File file) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2000-2017 Ericsson Telecom AB
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/

import java.io.File;
import java.io.IOException;

public class Main {
    private static boolean deleteIfExists(final File file) {
        return file.exists() && !file.delete();
    }// w  w  w.  j  a  v a2s .co  m

    /**
     * Deletes a file.
     * @param file the file to delete
     * @throws IOException if the file cannot be deleted
     */
    public static void delete(final File file) throws IOException {
        if (deleteIfExists(file)) {
            throw new IOException("The file cannot be deleted: " + file.getAbsolutePath());
        }
    }
}

Related

  1. deleteIfExists(File f)
  2. deleteIfExists(File file)
  3. deleteIfExists(File file)
  4. deleteIfExists(File file)
  5. deleteIfExists(final File file)
  6. deleteIfExists(java.io.File file)