Here you can find the source of deleteFolder(String sPath)
Parameter | Description |
---|---|
sPath | a parameter |
public static boolean deleteFolder(String sPath)
//package com.java2s; /**//from w ww .j av a 2 s . co m * Copyright (c) 2017 Institute of Computing Technology, Chinese Academy of Sciences, 2017 * Institute of Computing Technology, Chinese Academy of Sciences contributors. All rights reserved. * * 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. See accompanying * LICENSE file. */ import java.io.File; public class Main { /** * *@param sPath *@return */ public static boolean deleteFolder(String sPath) { boolean flag = false; File file = new File(sPath); if (!file.exists()) { return flag; } else { if (file.isFile()) { return deleteFile(sPath); } else { return deleteDirectory(sPath); } } } /** * * @param sPath * @return */ public static boolean deleteFile(String sPath) { boolean flag = false; File file = new File(sPath); if (file.isFile() && file.exists()) { file.delete(); flag = true; } return flag; } /** * * @param sPath * @return */ private static boolean deleteDirectory(String sPath) { if (!sPath.endsWith(File.separator)) { sPath = sPath + File.separator; } File dirFile = new File(sPath); if (!dirFile.exists() || !dirFile.isDirectory()) { return false; } boolean flag = true; File[] files = dirFile.listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].isFile()) { flag = deleteFile(files[i].getAbsolutePath()); if (!flag) break; } else { flag = deleteDirectory(files[i].getAbsolutePath()); if (!flag) break; } } if (!flag) return false; if (dirFile.delete()) { return true; } else { return false; } } }