Java File Path Delete delete(String filePath, boolean recursive)

Here you can find the source of delete(String filePath, boolean recursive)

Description

delete

License

Apache License

Declaration

public static boolean delete(String filePath, boolean recursive) 

Method Source Code

//package com.java2s;
/*****************************************************************
 *   Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.//  ww w  .j  a va 2 s .com
 ****************************************************************/

import java.io.File;

public class Main {
    public static boolean delete(String filePath, boolean recursive) {
        File file = new File(filePath);
        if (!file.exists()) {
            return true;
        }

        if (!recursive || !file.isDirectory())
            return file.delete();

        String[] contents = file.list();

        // list can be null if directory doesn't have an 'x' permission bit set for the
        // user
        if (contents != null) {
            for (String item : contents) {
                if (!delete(filePath + File.separator + item, true)) {
                    return false;
                }
            }
        }

        return file.delete();
    }
}

Related

  1. delete(String fileNameWithFullPath)
  2. delete(String filePath)
  3. delete(String filePath)
  4. delete(String filePath)
  5. delete(String filePath)
  6. delete(String path)
  7. delete(String path)
  8. delete(String path)
  9. delete(String path)