JAVA copier un dossier, manipulation arborescence

De Wiki de Romain RUDIGER
Aller à : navigation, rechercher

JAVA Copier un dossier / Manipulation d'arborescence

Le but de ce tutoriel JAVA est de fournir une classe contenant des fonctions utiles à la gestion de fichiers. Exemples: Copier le contenu d'un dossier dans un autre dossier, copier des fichiers d'un dossier vers un autre dossier, copier un seul fichier vers un dossier, renommer un fichier, copier un répertoire, ...

JAVA - FileTools Class

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * FileTool is a class which provides some methods used to work on Files and Folders.
 */
public final class FileTools {

	/**
	 * FileTools Constructor.
	 */
	public FileTools() {
	}

	/**
	 * Check if a specified file path is a folder and create a folder if it does
	 * not exist.
	 * 
	 * @param folderPath A folder path.
	 * @param logger A logger.
	 */
	public static void checkFolder(String folderPath, Logger logger) {
		File file = new File(folderPath);
		if (!(file.isDirectory())) {
			file.mkdir();
			logger.fine(Messages.create_directory + file.getPath());
		}
	}

	/**
	 * Delete a directory.
	 * 
	 * @param path A folder path.
	 * @param logger A logger.
	 */
	public static void deleteDirectory(File path, Logger logger) {
		if (path.exists()) {
			File[] files = path.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					deleteDirectory(files[i], logger);
				} else {
					files[i].delete();
					logger.finest(Messages.file_remove
							+ files[i].getAbsolutePath());
				}
			}
		}
		path.delete();
		logger.finest(Messages.dir_removed + path.getAbsolutePath());
	}

	/**
	 * Move a file from a source to a destination. If the moving by using the
	 * renameTo method does not work, it used the copy method.
	 * 
	 * @param src Source file path.
	 * @param dst Destination file path.
	 * @param logger A logger.
	 */
	public static void move(String src, String dst, Logger logger) {
		File source = new File(src);
		File destination = new File(dst);
		// Try to use renameTo
		boolean result = source.renameTo(destination);
		if (!result) {
			// Copy
			copy(src, dst, logger);
		} else {
			logger.finest(Messages.renameTo_end_process);
		}
	}

	/**
	 * Copy a file from a source to a destination.
	 * 
	 * @param src Source file path.
	 * @param dst Destination file path.
	 * @param logger A logger.
	 */
	public static void copy(String src, String dst, Logger logger) {
		logger.finest(Messages.copy_process);
		File source = new File(src);
		File destination = new File(dst);
		FileInputStream sourceFile = null;
		FileOutputStream destinationFile = null;
		try {
			destination.createNewFile();
			sourceFile = new FileInputStream(source);
			destinationFile = new java.io.FileOutputStream(destination);
			// Read by 0.5MB segment.
			byte buffer[] = new byte[512 * 1024];
			int nbRead;
			while ((nbRead = sourceFile.read(buffer)) != -1) {
				destinationFile.write(buffer, 0, nbRead);
			}
			sourceFile.close();
			destinationFile.close();
		} catch (Exception e) {
			logger.log(Level.SEVERE, Messages.uncaught_exception_message, e);
		}
	}

	/**
	 * Copy all files and directories from a Folder to a destination Folder.
	 * Must be called like: listAllFilesInFolder(srcFolderPath, "", srcFolderPath,
	 * destFolderPath)
	 * 
	 * @param currentFolder Used for the recursive called.
	 * @param relatedPath Used for the recursive called.
	 * @param sourceFolder Source directory.
	 * @param destinationFolder Destination directory.
	 * @param logger A logger.
	 */
	public static void copyFolderToFolder(String currentFolder,
			String relatedPath, String sourceFolder, String destinationFolder,
			Logger logger) {
		// Current Directory.
		logger.finest("currentFolder: " + currentFolder + ", relatedPath: "
				+ relatedPath);
		File current = new File(currentFolder);
		if (current.isDirectory()) {
			// List all files and folder in the current directory.
			File[] list = current.listFiles();
			if (list != null) {
				// Read the files list.
				for (int i = 0; i < list.length; i++) {
					// Create current source File
					File tf = new File(sourceFolder + relatedPath + "\\"
							+ list[i].getName());
					// Create current destination File
					File pf = new File(destinationFolder + relatedPath + "\\"
							+ list[i].getName());
					if (tf.isDirectory() && !pf.exists()) {
						// If the file is a directory and does not exit in the
						// destination Folder.
						logger.finest(Messages.create_directory
								+ pf.getAbsolutePath());
						// Create the directory.
						pf.mkdir();
						copyFolderToFolder(tf.getAbsolutePath(), relatedPath
								+ "\\" + tf.getName(), sourceFolder,
								destinationFolder, logger);
					} else if (tf.isDirectory() && pf.exists()) {
						// If the file is a directory and exits in the
						// destination Folder.
						copyFolderToFolder(tf.getAbsolutePath(), relatedPath
								+ "\\" + tf.getName(), sourceFolder,
								destinationFolder, logger);
					} else if (tf.isFile()) {
						// If it is a file.
						logger.finest(Messages.file_copy + sourceFolder
								+ relatedPath + "\\" + list[i].getName()
								+ Messages.file_copy_to + destinationFolder
								+ relatedPath + "\\" + list[i].getName());
						copy(sourceFolder + relatedPath + "\\"
								+ list[i].getName(), destinationFolder
								+ relatedPath + "\\" + list[i].getName(),
								logger);
					} else {
						// Other cases.
						logger.severe(Messages.file_problem
								+ tf.getAbsolutePath());
					}
				}
			}
		}
	}

	/**
	 * Remove a Vector of files on the local machine.
	 * 
	 * @param files A vector of file paths.
	 * @param projectDirectory The project Directory.
	 * @param logger A logger.
	 */
	public static void removeFiles(Vector<String> files,
			String projectDirectory, Logger logger) {
		Iterator<String> it = files.iterator();
		logger.fine(Messages.remove_files_vector);
		while (it.hasNext()) {
			removeFile(it.next(), projectDirectory, logger);
		}
	}

	/**
	 * Remove a file in a specified root directory.
	 * 
	 * @param file A file path.
	 * @param rootDirectory A root directory.
	 * @param logger A logger.
	 */
	public static void removeFile(String file, String rootDirectory,
			Logger logger) {
		// Remove a file on the local machine
		if (file.equalsIgnoreCase("") || file == null) {
			logger.finest(Messages.no_file_to_remove);
		}
		File dir = new File(rootDirectory);
		if (!dir.isDirectory()) {
			logger.severe(Messages.not_a_folder_message + rootDirectory);
		} else {
			String filename;
			if (rootDirectory.charAt(rootDirectory.length() - 1) == '/') {
				filename = rootDirectory + file;
			} else {
				filename = rootDirectory + "/" + file;
			}
			File f = new File(filename);
			if (f.exists()) {
				f.delete();
				logger.finest(Messages.file_remove + filename);
			} else {
				logger.finest(Messages.file_already_remove
						+ f.getAbsolutePath());
			}
		}
	}
}