Java File Path Delete deleteAllFilesOnlyInDirectory(File startingDir)

Here you can find the source of deleteAllFilesOnlyInDirectory(File startingDir)

Description

delete All Files Only In Directory

License

Open Source License

Declaration

public static void deleteAllFilesOnlyInDirectory(File startingDir) 

Method Source Code


//package com.java2s;
/*//from ww w.j a v  a  2 s.  co m
    
The Martus(tm) free, social justice documentation and
monitoring software. Copyright (C) 2001-2007, Beneficent
Technology, Inc. (The Benetech Initiative).
    
Martus is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later
version with the additions and exceptions described in the
accompanying Martus license file entitled "license.txt".
    
It is distributed WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, including warranties of fitness of purpose or
merchantability.  See the accompanying Martus License and
GPL license for more details on the required license terms
for this software.
    
You should have received a copy of the GNU General Public
License along with this program; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
    
To the extent this copyrighted software code is used in the 
Miradi project, it is subject to a royalty-free license to 
members of the Conservation Measures Partnership when 
used with the Miradi software as specified in the agreement 
between Benetech and WCS dated 5/1/05.
*/

import java.io.File;

public class Main {
    public static void deleteAllFilesOnlyInDirectory(File startingDir) {
        File[] filesToDelete = startingDir.listFiles();
        if (filesToDelete != null) {
            for (int i = 0; i < filesToDelete.length; i++) {
                if (filesToDelete[i].isFile() && !filesToDelete[i].delete())
                    System.out.println("Unable to delete file: " + filesToDelete[i].getPath());
            }
        }
    }

    static public File[] listFiles(File directory) {
        File[] files = directory.listFiles();
        if (files == null)
            return new File[0];
        return files;
    }
}

Related

  1. deleteAllFiles(Vector filesToDelete)
  2. deleteAllFilesAux(String input, File swapFolder)
  3. deleteAllFilesIn(String dir)
  4. deleteAllFilesInDir(File dir)
  5. deleteAllFilesInDirectory(String path)
  6. deleteAllFilesRecursively(String directoryName)
  7. deleteAndCreateFolder(String path)
  8. deleteBlankPath(String path)
  9. deleteChildDirectoriesExcept(File path, String... directoriesToSkip)