Android examples for java.io:Directory
copies all files within a directory to another directory
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import android.util.Log; public class Main { private static final String TAG = ""; /**//from w w w .j a va 2 s .c om * copies all files within a directory to another directory * * @param destinationDirectory * the target directory * @param sourceDirectory * the source directory * @return true if all contents were copied successfully, false otherwise */ public static boolean copyDirectoryContents(File destinationDirectory, File sourceDirectory) { if (destinationDirectory == null) { Log.e(TAG, "Unable to copy: destinationDirectory is null"); return false; } if (sourceDirectory == null) { Log.e(TAG, "Unable to copy: sourceDirectory is null"); return false; } // If the source and destination directories exist then copy the files if (sourceDirectory.exists() && sourceDirectory.isDirectory() && destinationDirectory.exists() && destinationDirectory.isDirectory() && destinationDirectory.canWrite()) { List<String> failedCopy = null; for (File fileToCopy : sourceDirectory.listFiles()) { // Find and copy the file to the output directory Log.i(TAG, "Copying link file [" + fileToCopy.getName() + "] from [" + sourceDirectory.getAbsolutePath() + "] to [" + destinationDirectory + "]"); if (!copyFile(destinationDirectory, fileToCopy, fileToCopy.getName())) { if (failedCopy == null) { failedCopy = new ArrayList<String>(); } failedCopy.add(fileToCopy.getName()); } } if (failedCopy != null) { // Report on the files that could not be copied Log.w(TAG, "Failed to copy the following files: "); for (String fileName : failedCopy) { Log.w(TAG, "\t [" + fileName + "]"); } } else { return true; } } return false; } /** * Copy copy file sourceFile to the directory destination directory * * @param destinationDirectory * location where the file to be copied * @param sourceFile * the location of the file to copy * @param targetFileName * name of the target file * @return true if the file was copied successfully, false otherwise */ public static boolean copyFile(final File destinationDirectory, final File sourceFile, final String targetFileName) { boolean _return = false; if (null != destinationDirectory && null != sourceFile) { FileInputStream inputStream = null; FileOutputStream outputStream = null; byte[] dataBuffer = new byte[1024]; File outputFile = new File(destinationDirectory.getAbsoluteFile() + File.separator + targetFileName); try { inputStream = new FileInputStream(sourceFile); outputStream = new FileOutputStream(outputFile); try { int bytesRead = inputStream.read(dataBuffer); while (-1 != bytesRead) { outputStream.write(dataBuffer, 0, bytesRead); bytesRead = inputStream.read(dataBuffer); } // No errors copying the file, look like we're good _return = true; } catch (IOException e) { Log.w(TAG, "IOException trying to write copy file [" + sourceFile.getAbsolutePath() + "] to [" + destinationDirectory.getAbsolutePath() + "]: [" + e.getMessage() + "]"); } } catch (FileNotFoundException e) { Log.w(TAG, "File not found exception trying to write copy file [" + sourceFile.getAbsolutePath() + "] to [" + destinationDirectory.getAbsolutePath() + "]: [" + e.getMessage() + "]"); } } return _return; } }