1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.android.inputmethod.latin.common; 18 19 import java.io.File; 20 import java.io.FilenameFilter; 21 22 /** 23 * A simple class to help with removing directories recursively. 24 */ 25 public class FileUtils { 26 private static final String TAG = "FileUtils"; 27 28 public static boolean deleteRecursively(final File path) { 29 if (path.isDirectory()) { 30 final File[] files = path.listFiles(); 31 if (files != null) { 32 for (final File child : files) { 33 deleteRecursively(child); 34 } 35 } 36 } 37 return path.delete(); 38 } 39 40 public static boolean deleteFilteredFiles(final File dir, final FilenameFilter fileNameFilter) { 41 if (!dir.isDirectory()) { 42 return false; 43 } 44 final File[] files = dir.listFiles(fileNameFilter); 45 if (files == null) { 46 return false; 47 } 48 boolean hasDeletedAllFiles = true; 49 for (final File file : files) { 50 if (!deleteRecursively(file)) { 51 hasDeletedAllFiles = false; 52 } 53 } 54 return hasDeletedAllFiles; 55 } 56 57 public static boolean renameTo(final File fromFile, final File toFile) { 58 toFile.delete(); 59 return fromFile.renameTo(toFile); 60 } 61 } 62