File工具类

public class FileUtil {
	private static final int BYTE_SIZE = 1024;

	/**
	 * 缓冲复制
	 * 
	 * @param sourceFile
	 *            源文件绝对路径
	 * @param targetFile
	 *            目标文件绝对路径
	 * @throws IOException
	 */
	public static void copFile(String sourceFile, String targetFile)
			throws IOException {
		FileInputStream fis = new FileInputStream(sourceFile);
		BufferedInputStream bis = new BufferedInputStream(fis);
		FileOutputStream fos = new FileOutputStream(targetFile);
		BufferedOutputStream bos = new BufferedOutputStream(fos);

		byte[] b = new byte[BYTE_SIZE];
		int len = 0;
		while ((len = bis.read(b)) != -1) {
			bos.write(b, 0, len);
		}
		fos.close();
		fis.close();
	}

	/**
	 * 删除文件夹下所有文件--慎用
	 * 
	 * @param fileDir
	 */
	public static void deleteAll(File fileDir) {
		File[] files = fileDir.listFiles();
		for (File file : files) {
			if (file.isDirectory()) {
				deleteAll(file);
			} else {
				file.delete();
			}
		}
		fileDir.delete();
	}

	public static void main(String[] args) throws IOException {
		// copFile("d:\\aaa\\AA.txt", "D:\\aaa\\BB.txt");
		deleteAll(new File("D:\\aaa"));
	}
}


你可能感兴趣的:(File,Inputstream)