001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.util; 018 019import java.io.File; 020import java.io.IOException; 021 022/** 023 * Utility methods for working with the file system. 024 * 025 * @author Rob Harrop 026 * @author Juergen Hoeller 027 * @since 2.5.3 028 */ 029public abstract class FileSystemUtils { 030 031 /** 032 * Delete the supplied {@link File} - for directories, 033 * recursively delete any nested directories or files as well. 034 * @param root the root {@code File} to delete 035 * @return {@code true} if the {@code File} was deleted, 036 * otherwise {@code false} 037 */ 038 public static boolean deleteRecursively(File root) { 039 if (root != null && root.exists()) { 040 if (root.isDirectory()) { 041 File[] children = root.listFiles(); 042 if (children != null) { 043 for (File child : children) { 044 deleteRecursively(child); 045 } 046 } 047 } 048 return root.delete(); 049 } 050 return false; 051 } 052 053 /** 054 * Recursively copy the contents of the {@code src} file/directory 055 * to the {@code dest} file/directory. 056 * @param src the source directory 057 * @param dest the destination directory 058 * @throws IOException in the case of I/O errors 059 */ 060 public static void copyRecursively(File src, File dest) throws IOException { 061 Assert.isTrue(src != null && (src.isDirectory() || src.isFile()), 062 "Source File must denote a directory or file"); 063 Assert.notNull(dest, "Destination File must not be null"); 064 doCopyRecursively(src, dest); 065 } 066 067 /** 068 * Actually copy the contents of the {@code src} file/directory 069 * to the {@code dest} file/directory. 070 * @param src the source directory 071 * @param dest the destination directory 072 * @throws IOException in the case of I/O errors 073 */ 074 private static void doCopyRecursively(File src, File dest) throws IOException { 075 if (src.isDirectory()) { 076 dest.mkdir(); 077 File[] entries = src.listFiles(); 078 if (entries == null) { 079 throw new IOException("Could not list files in directory: " + src); 080 } 081 for (File entry : entries) { 082 doCopyRecursively(entry, new File(dest, entry.getName())); 083 } 084 } 085 else if (src.isFile()) { 086 try { 087 dest.createNewFile(); 088 } 089 catch (IOException ex) { 090 throw new IOException("Failed to create file: " + dest, ex); 091 } 092 FileCopyUtils.copy(src, dest); 093 } 094 else { 095 // Special File handle: neither a file not a directory. 096 // Simply skip it when contained in nested directory... 097 } 098 } 099 100}