Java File Delete delete(File f)

Here you can find the source of delete(File f)

Description

delete

License

Apache License

Declaration

public static void delete(File f) 

Method Source Code

//package com.java2s;
/*// www  .  j a va  2 s .co  m
   Copyright 2014 Waritnan Sookbuntherng
    
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

import java.io.File;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void delete(File f) {
        if (f.exists()) {
            if (f.isDirectory())
                for (File f2 : listAllFiles(f))
                    delete(f2);
            f.delete();
        }
    }

    public static File[] listAllFiles(File dir) {
        if (!dir.exists())
            return null;
        Set<File> fs = new HashSet<File>(Arrays.asList(dir.listFiles()));
        for (File f : fs.toArray(new File[fs.size()])) {
            if (f.isDirectory())
                fs.addAll(Arrays.asList(listAllFiles(f)));
            fs.add(f);
        }
        return fs.toArray(new File[fs.size()]);
    }
}

Related

  1. delete(File dir)
  2. delete(File dir)
  3. delete(File f)
  4. delete(File f)
  5. delete(File f)
  6. delete(File f)
  7. delete(File f)
  8. delete(File f)
  9. delete(File f)