Java Folder Delete emptyFile(final String urlFile)

Here you can find the source of emptyFile(final String urlFile)

Description

Empty a file.

License

Open Source License

Parameter

Parameter Description
urlFile The url file.

Exception

Parameter Description
IOException if the file does not exist or cannot be read.

Declaration

public static void emptyFile(final String urlFile) throws IOException 

Method Source Code

//package com.java2s;
/*/*from w  w  w. j a  va2 s . c  om*/
 *    Geotoolkit - An Open Source Java GIS Toolkit
 *    http://www.geotoolkit.org
 *
 *    (C) 2008 - 2009, Geomatys
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * Empty a file.
     *
     * @param urlFile The url file.
     *
     * @throws IOException if the file does not exist or cannot be read.
     */
    public static void emptyFile(final String urlFile) throws IOException {
        final File file = new File(urlFile);
        if (file.exists()) {
            if (!file.delete()) {
                throw new IOException("Failed to delete file : " + file);
            }
        }
        if (!file.createNewFile()) {
            //should have been deleted before, so we expect this to be true.
            throw new IOException("Failed to create file : " + file);
        }
    }
}

Related

  1. clearFolder(String path)
  2. clearFolderContent(String folderName)
  3. deepDelete(final File file)
  4. emptyAndDelete(File fileOrDir)
  5. emptyDerbyTestDirectory(final String derbyTestDirectory)
  6. emptyFile(String srcFilename)
  7. emptyFolder(File dir)
  8. emptyFolder(File folder)
  9. emptyFolder(File folder)