directory Move - Android File Input Output

Android examples for File Input Output:Directory

Description

directory Move

Demo Code


import android.annotation.SuppressLint;
import android.util.Log;
import java.io.*;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Pattern;

public class Main{
    private static final String TAG = SystemUtils.getClassName();
    public static void directoryMove(File sourceLocation,
            File targetLocation, Pattern pattern) throws IOException {
        if (sourceLocation == null || !sourceLocation.exists()) {
            return;
        }//  w  ww  .  j a va 2  s.  c o m
        if (sourceLocation.isDirectory()) {
            String[] children = sourceLocation.list();
            for (int i = 0; i < children.length; i++) {
                File sourceChildren = new File(sourceLocation, children[i]);
                File targetChildren = new File(targetLocation, children[i]);
                if ((pattern == null || pattern.matcher(children[i])
                        .matches())
                        && !isSubDirectory(sourceChildren, targetChildren)) {
                    directoryMove(new File(sourceLocation, children[i]),
                            targetLocation, pattern);
                }
            }
            sourceLocation.delete();
        } else if ((pattern == null || pattern.matcher(
                sourceLocation.getName()).matches())) {
            sourceLocation.delete();
        }
    }
    public static boolean isSubDirectory(File sourceLocation,
            File targetLocation) {
        return Pattern.matches(sourceLocation.getAbsolutePath() + ".*",
                targetLocation.getAbsolutePath());
    }
    public static boolean delete(File file) {
        if (file != null && file.exists()) {
            if (!file.delete()) {
                Log.w(TAG, "Failed to delete file " + file);
                return false;
            }
            Log.i(TAG, "Deleted file " + file);
        }
        return true;
    }
}

Related Tutorials