Here you can find the source of deleteAllFilesAux(String input, File swapFolder)
swapFolder
.
Parameter | Description |
---|---|
input | The name of folder (origin) whose files will be removed and backed up. |
swapFolder | The temporary folder used as swap. |
Parameter | Description |
---|---|
IOException | Thrown when it is not possible to copy a file. |
private static File deleteAllFilesAux(String input, File swapFolder) throws IOException
//package com.java2s; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /**/* w w w . j a v a2 s . c om*/ * Deletes all files in inputFolder backing them up in <code>swapFolder</code>. This method * had to be implemented since file.canWrite() together with file.delete() do not work properly * due to a JAVA bug. See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4041126. * * @param input The name of folder (origin) whose files will be removed and backed up. * @param swapFolder The temporary folder used as swap. * @return The first file that could not be deleted. * @throws IOException Thrown when it is not possible to copy a file. */ private static File deleteAllFilesAux(String input, File swapFolder) throws IOException { File result = null; /* creates a file from the input */ File file = new File(input); if (file.exists()) // verifies if the file exists { if (file.isDirectory()) // checks if it is a directory { File originalFolder = file; // keeps a reference from the current folder File swap = null; /* * creates the directory trying to keep the same directory structure in the swap * folder */ swap = new File(swapFolder + getSeparator() + file.getName()); swap.mkdir(); /* Iterates over all elements of the current directory 'file' */ File[] files = file.listFiles(); for (File file2 : files) { result = deleteAllFilesAux(file2.getAbsolutePath(), swap); if (result != null) { break; } } if (result == null) { originalFolder.delete(); } } else { /* copies the file to the swap directory */ copyFiles(new String[] { file.getAbsolutePath() }, swapFolder.getAbsolutePath(), false); /* tries to delete the current file */ if (!file.delete()) { result = file; } else { result = null; } } } return result; } /** * Returns the system separator. * * @return The system separator string. */ public static String getSeparator() { return System.getProperty("file.separator"); } /** * Copy a list of files to a specifc folder. * * @param sourceFiles An array containing the name of the files to be copied. * @param toDir The destination forlder. * @param overwrite Indicates if the existing files in the destination folder will be * overwritten. * @return The new absolute file names. * @throws IOException If there is a file in the destination folder with same source name. */ public static String[] copyFiles(String[] sourceFiles, String toDir, boolean overwrite) throws IOException { String[] newFileNames = new String[sourceFiles.length]; for (int i = 0; i < sourceFiles.length; i++) { File inFile = new File(sourceFiles[i]); File outFile = new File(toDir + getSeparator() + inFile.getName()); boolean fileExists = false; if (outFile.exists()) { fileExists = true; if (overwrite) { if (!outFile.delete()) { throw new IOException(outFile.getAbsolutePath() + " cannot be overwritten."); } fileExists = false; } } if (!fileExists) { byte[] b = new byte[(int) inFile.length()]; InputStream in = new FileInputStream(inFile); OutputStream out = new FileOutputStream(outFile); in.read(b); out.write(b); out.close(); in.close(); newFileNames[i] = outFile.getAbsolutePath(); } } return newFileNames; // return the new locations } }