Android File Move moveFile(String in, String out)

Here you can find the source of moveFile(String in, String out)

Description

Moves a file, overwriting any existing file at the destination.

Declaration

static public String moveFile(String in, String out) 

Method Source Code

import java.io.*;
import java.nio.channels.*;

public class Main{
    /**/*from www  .j a  v  a 2  s .  c om*/
     * Moves a file, overwriting any existing file at the destination.
     */
    static public String moveFile(String in, String out) {
        copyFile(in = assertNotEmpty("in", in), out);
        delete(in);
        return out;
    }
    /**
     * Copies one file to another.
     */
    @SuppressWarnings({ "ResultOfMethodCallIgnored" })
    public static void copyFile(File in, File out) {
        assertNotNull("in", in);
        assertNotNull("out", out);
        LOGGER.trace.log("Copying file: ", in, " -> ", out);
        FileChannel sourceChannel = createFileInputStream(in).getChannel();
        out.getParentFile().mkdirs();
        try {
            FileChannel destinationChannel = createFileOutputStream(out)
                    .getChannel();
            try {
                sourceChannel.transferTo(0, sourceChannel.size(),
                        destinationChannel);
                Closeable zCloseable = destinationChannel;
                destinationChannel = null;
                close(zCloseable);
            } catch (IOException e) {
                throw new WrappedIOException(e);
            } finally {
                dispose(destinationChannel);
            }
        } finally {
            dispose(sourceChannel);
        }
    }
    /**
     * Copies a file, overwriting any existing file at the destination.
     */
    public static String copyFile(String in, String out) {
        copyFile(new File(assertNotEmpty("in", in)), new File(
                out = assertNotEmpty("out", out)));
        return out;
    }
    /**
     * Deletes a directory and all files and directories it contains.
     */
    public static boolean delete(File pFile) {
        assertNotNull("File", pFile);
        if (pFile.isDirectory()) {
            File[] zFiles = pFile.listFiles();
            for (File zFile : zFiles) {
                if (!delete(zFile)) {
                    return false;
                }
            }
        }
        LOGGER.trace.log("Deleting file: ", pFile);
        return pFile.delete();
    }
    /**
     * Deletes a file or directory and all files and subdirecties under it.
     */
    public static boolean delete(String fileName) {
        fileName = noEmpty(fileName);
        return (fileName != null) && delete(new File(fileName));
    }
    public static FileInputStream createFileInputStream(File in) {
        try {
            return new FileInputStream(in);
        } catch (FileNotFoundException e) {
            throw new WrappedIOException(e);
        }
    }
    public static FileOutputStream createFileOutputStream(File out) {
        mkdir(out.getParentFile());
        try {
            return new FileOutputStream(out);
        } catch (FileNotFoundException e) {
            throw new WrappedIOException(e);
        }
    }
    public static void close(Closeable pCloseable) {
        if (pCloseable != null) {
            try {
                pCloseable.close();
            } catch (IOException e) {
                throw new WrappedIOException(e);
            }
        }
    }
    public static Closeable dispose(Closeable pCloseable) {
        if (pCloseable != null) {
            try {
                pCloseable.close();
            } catch (IOException ignore) {
                // Whatever!
            }
            pCloseable = null;
        }
        return pCloseable;
    }
    /**
     * Creates the directories in the specified path.
     */
    public static File mkdir(File path) {
        assertNotNull("path", path);
        if (path.mkdirs()) {
            LOGGER.trace.log("Created directory: ", path.getPath());
        }
        return path;
    }
    /**
     * Creates the directories in the specified path.
     */
    public static String mkdir(String path) {
        mkdir(new File(path = assertNotEmpty("path", path)));
        return path;
    }
}

Related

  1. moveFile(String sourceFileName, String destPath)
  2. moveSubFiles(String sourceFileName, String destPath)
  3. move(String input, String output)
  4. moveFile(File source, File target)
  5. moveFile(File src, File dest)