Here you can find the source of copyFilesBinaryRecursively(File fromLocation, File toLocation, FileFilter fileFilter)
Parameter | Description |
---|---|
fromLocation | a parameter |
toLocation | a parameter |
fileFilter | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static void copyFilesBinaryRecursively(File fromLocation, File toLocation, FileFilter fileFilter) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2007-2009 Red Hat, Inc. * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html * * Contributor://w ww . j av a 2 s.c o m * Red Hat, Inc. - initial API and implementation ******************************************************************************/ import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Main { /** * Recursively copies files and subdirectories from fromLocation to * toLocation using FileFilter fileFliter * * @param fromLocation * @param toLocation * @param fileFilter * @throws IOException */ public static void copyFilesBinaryRecursively(File fromLocation, File toLocation, FileFilter fileFilter) throws IOException { if (fromLocation.exists()) { for (File fileToCopy : fromLocation.listFiles(fileFilter)) { if (fileToCopy.isDirectory()) { File newToDir = new File(toLocation, fileToCopy.getName()); newToDir.mkdir(); copyFilesBinaryRecursively(fileToCopy, newToDir, fileFilter); } else { copyFilesBinary(fileToCopy, toLocation); } } } } /** * Copies binary file originalFile to location toLocation * * @param originalFile * @param toLocation * @throws IOException */ public static void copyFilesBinary(File originalFile, File toLocation) throws IOException { FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(originalFile); fos = new FileOutputStream(new File(toLocation, originalFile.getName())); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { fos.write(buffer, 0, bytesRead); // write } } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { // do nothing } } if (fos != null) { try { fos.flush(); fos.close(); } catch (IOException e) { // do nothing } } } } }