Java Delete File deleteFile(String filename)

Here you can find the source of deleteFile(String filename)

Description

Utility method to delete a file if it exists.

License

Open Source License

Parameter

Parameter Description
file file to delete

Exception

Parameter Description
Exception if the file couldn't be deleted for any reason

Declaration

public static void deleteFile(String filename) throws Exception 

Method Source Code


//package com.java2s;
/*-/*from ww  w.  j  av a2  s  .  c o m*/
 *******************************************************************************
 * Copyright (c) 2011, 2016 Diamond Light Source Ltd.
 * 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
 *
 * Contributors:
 *    Matthew Gerring - initial API and implementation and/or initial documentation
 *******************************************************************************/

import java.io.File;

public class Main {
    /**
     * Utility method to delete a file if it exists.
     * @param file file to delete
     * @throws Exception if the file couldn't be deleted for any reason
     */
    public static void deleteFile(String filename) throws Exception {
        File file = new File(filename);
        if (file.exists()) {
            if (!file.delete()) {
                throw new Exception("Could not delete file " + file);
            }
        }
    }
}

Related

  1. deleteFile(String f)
  2. deleteFile(String file)
  3. deleteFile(String file)
  4. deleteFile(String file)
  5. deleteFile(String file)
  6. deleteFile(String fileName)
  7. deleteFile(String fileName)
  8. deleteFile(String fileName)
  9. deleteFile(String filename)