首页

分享spring对文件递归复制、删除等系统操作工具类FileSystemUtils及FileCopyUtils

标签:springframework,FileCopyUtils,FileSystemUtils,递归操作,文件系统工具类,迭代删除,循环复制     发布时间:2017-11-30   

一、前言

这边基于spring-core提供的org.springframework.util.FileSystemUtils、org.springframework.util.FileCopyUtils等工具类对系统文件进行递归复制、递归删除等

二、代码

1.FileSystemUtils文件系统工具类

package org.springframework.util;@b@@b@import java.io.File;@b@import java.io.IOException;@b@@b@/**@b@ * Utility methods for working with the file system.@b@ *@b@ * @author Rob Harrop@b@ * @author Juergen Hoeller@b@ * @since 2.5.3@b@ */@b@public abstract class FileSystemUtils {@b@@b@	/**@b@	 * Delete the supplied {@link File} - for directories,@b@	 * recursively delete any nested directories or files as well.@b@	 * @param root the root {@code File} to delete@b@	 * @return {@code true} if the {@code File} was deleted,@b@	 * otherwise {@code false}@b@	 */@b@	public static boolean deleteRecursively(File root) {@b@		if (root != null && root.exists()) {@b@			if (root.isDirectory()) {@b@				File[] children = root.listFiles();@b@				if (children != null) {@b@					for (File child : children) {@b@						deleteRecursively(child);@b@					}@b@				}@b@			}@b@			return root.delete();@b@		}@b@		return false;@b@	}@b@@b@	/**@b@	 * Recursively copy the contents of the {@code src} file/directory@b@	 * to the {@code dest} file/directory.@b@	 * @param src the source directory@b@	 * @param dest the destination directory@b@	 * @throws IOException in the case of I/O errors@b@	 */@b@	public static void copyRecursively(File src, File dest) throws IOException {@b@		Assert.isTrue(src != null && (src.isDirectory() || src.isFile()), "Source File must denote a directory or file");@b@		Assert.notNull(dest, "Destination File must not be null");@b@		doCopyRecursively(src, dest);@b@	}@b@@b@	/**@b@	 * Actually copy the contents of the {@code src} file/directory@b@	 * to the {@code dest} file/directory.@b@	 * @param src the source directory@b@	 * @param dest the destination directory@b@	 * @throws IOException in the case of I/O errors@b@	 */@b@	private static void doCopyRecursively(File src, File dest) throws IOException {@b@		if (src.isDirectory()) {@b@			dest.mkdir();@b@			File[] entries = src.listFiles();@b@			if (entries == null) {@b@				throw new IOException("Could not list files in directory: " + src);@b@			}@b@			for (File entry : entries) {@b@				doCopyRecursively(entry, new File(dest, entry.getName()));@b@			}@b@		}@b@		else if (src.isFile()) {@b@			try {@b@				dest.createNewFile();@b@			}@b@			catch (IOException ex) {@b@				IOException ioex = new IOException("Failed to create file: " + dest);@b@				ioex.initCause(ex);@b@				throw ioex;@b@			}@b@			FileCopyUtils.copy(src, dest);@b@		}@b@		else {@b@			// Special File handle: neither a file not a directory.@b@			// Simply skip it when contained in nested directory...@b@		}@b@	}@b@@b@}

2.FileCopyUtils - 文件复制工具类

package org.springframework.util;@b@@b@import java.io.BufferedInputStream;@b@import java.io.BufferedOutputStream;@b@import java.io.ByteArrayInputStream;@b@import java.io.ByteArrayOutputStream;@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.OutputStream;@b@import java.io.Reader;@b@import java.io.StringWriter;@b@import java.io.Writer;@b@@b@/**@b@ * Simple utility methods for file and stream copying. All copy methods use a block size@b@ * of 4096 bytes, and close all affected streams when done. A variation of the copy@b@ * methods from this class that leave streams open can be found in {@link StreamUtils}.@b@ *@b@ * <p>Mainly for use within the framework, but also useful for application code.@b@ *@b@ * @author Juergen Hoeller@b@ * @since 06.10.2003@b@ * @see StreamUtils@b@ */@b@public abstract class FileCopyUtils {@b@@b@	public static final int BUFFER_SIZE = StreamUtils.BUFFER_SIZE;@b@@b@@b@	//---------------------------------------------------------------------@b@	// Copy methods for java.io.File@b@	//---------------------------------------------------------------------@b@@b@	/**@b@	 * Copy the contents of the given input File to the given output File.@b@	 * @param in the file to copy from@b@	 * @param out the file to copy to@b@	 * @return the number of bytes copied@b@	 * @throws IOException in case of I/O errors@b@	 */@b@	public static int copy(File in, File out) throws IOException {@b@		Assert.notNull(in, "No input File specified");@b@		Assert.notNull(out, "No output File specified");@b@		return copy(new BufferedInputStream(new FileInputStream(in)),@b@			new BufferedOutputStream(new FileOutputStream(out)));@b@	}@b@@b@	/**@b@	 * Copy the contents of the given byte array to the given output File.@b@	 * @param in the byte array to copy from@b@	 * @param out the file to copy to@b@	 * @throws IOException in case of I/O errors@b@	 */@b@	public static void copy(byte[] in, File out) throws IOException {@b@		Assert.notNull(in, "No input byte array specified");@b@		Assert.notNull(out, "No output File specified");@b@		ByteArrayInputStream inStream = new ByteArrayInputStream(in);@b@		OutputStream outStream = new BufferedOutputStream(new FileOutputStream(out));@b@		copy(inStream, outStream);@b@	}@b@@b@	/**@b@	 * Copy the contents of the given input File into a new byte array.@b@	 * @param in the file to copy from@b@	 * @return the new byte array that has been copied to@b@	 * @throws IOException in case of I/O errors@b@	 */@b@	public static byte[] copyToByteArray(File in) throws IOException {@b@		Assert.notNull(in, "No input File specified");@b@		return copyToByteArray(new BufferedInputStream(new FileInputStream(in)));@b@	}@b@@b@@b@	//---------------------------------------------------------------------@b@	// Copy methods for java.io.InputStream / java.io.OutputStream@b@	//---------------------------------------------------------------------@b@@b@	/**@b@	 * Copy the contents of the given InputStream to the given OutputStream.@b@	 * Closes both streams when done.@b@	 * @param in the stream to copy from@b@	 * @param out the stream to copy to@b@	 * @return the number of bytes copied@b@	 * @throws IOException in case of I/O errors@b@	 */@b@	public static int copy(InputStream in, OutputStream out) throws IOException {@b@		Assert.notNull(in, "No InputStream specified");@b@		Assert.notNull(out, "No OutputStream specified");@b@		try {@b@			return StreamUtils.copy(in, out);@b@		}@b@		finally {@b@			try {@b@				in.close();@b@			}@b@			catch (IOException ex) {@b@			}@b@			try {@b@				out.close();@b@			}@b@			catch (IOException ex) {@b@			}@b@		}@b@	}@b@@b@	/**@b@	 * Copy the contents of the given byte array to the given OutputStream.@b@	 * Closes the stream when done.@b@	 * @param in the byte array to copy from@b@	 * @param out the OutputStream to copy to@b@	 * @throws IOException in case of I/O errors@b@	 */@b@	public static void copy(byte[] in, OutputStream out) throws IOException {@b@		Assert.notNull(in, "No input byte array specified");@b@		Assert.notNull(out, "No OutputStream specified");@b@		try {@b@			out.write(in);@b@		}@b@		finally {@b@			try {@b@				out.close();@b@			}@b@			catch (IOException ex) {@b@			}@b@		}@b@	}@b@@b@	/**@b@	 * Copy the contents of the given InputStream into a new byte array.@b@	 * Closes the stream when done.@b@	 * @param in the stream to copy from@b@	 * @return the new byte array that has been copied to@b@	 * @throws IOException in case of I/O errors@b@	 */@b@	public static byte[] copyToByteArray(InputStream in) throws IOException {@b@		ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE);@b@		copy(in, out);@b@		return out.toByteArray();@b@	}@b@@b@@b@	//---------------------------------------------------------------------@b@	// Copy methods for java.io.Reader / java.io.Writer@b@	//---------------------------------------------------------------------@b@@b@	/**@b@	 * Copy the contents of the given Reader to the given Writer.@b@	 * Closes both when done.@b@	 * @param in the Reader to copy from@b@	 * @param out the Writer to copy to@b@	 * @return the number of characters copied@b@	 * @throws IOException in case of I/O errors@b@	 */@b@	public static int copy(Reader in, Writer out) throws IOException {@b@		Assert.notNull(in, "No Reader specified");@b@		Assert.notNull(out, "No Writer specified");@b@		try {@b@			int byteCount = 0;@b@			char[] buffer = new char[BUFFER_SIZE];@b@			int bytesRead = -1;@b@			while ((bytesRead = in.read(buffer)) != -1) {@b@				out.write(buffer, 0, bytesRead);@b@				byteCount += bytesRead;@b@			}@b@			out.flush();@b@			return byteCount;@b@		}@b@		finally {@b@			try {@b@				in.close();@b@			}@b@			catch (IOException ex) {@b@			}@b@			try {@b@				out.close();@b@			}@b@			catch (IOException ex) {@b@			}@b@		}@b@	}@b@@b@	/**@b@	 * Copy the contents of the given String to the given output Writer.@b@	 * Closes the writer when done.@b@	 * @param in the String to copy from@b@	 * @param out the Writer to copy to@b@	 * @throws IOException in case of I/O errors@b@	 */@b@	public static void copy(String in, Writer out) throws IOException {@b@		Assert.notNull(in, "No input String specified");@b@		Assert.notNull(out, "No Writer specified");@b@		try {@b@			out.write(in);@b@		}@b@		finally {@b@			try {@b@				out.close();@b@			}@b@			catch (IOException ex) {@b@			}@b@		}@b@	}@b@@b@	/**@b@	 * Copy the contents of the given Reader into a String.@b@	 * Closes the reader when done.@b@	 * @param in the reader to copy from@b@	 * @return the String that has been copied to@b@	 * @throws IOException in case of I/O errors@b@	 */@b@	public static String copyToString(Reader in) throws IOException {@b@		StringWriter out = new StringWriter();@b@		copy(in, out);@b@		return out.toString();@b@	}@b@@b@}