Java File Move nui move(File in, File out)

Here you can find the source of move(File in, File out)

Description

move

License

Open Source License

Declaration

public static void move(File in, File out) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class Main {
    public static void move(File in, File out) throws IOException {
        copy(in, out);/*  w w  w.j  a v a  2  s .c o  m*/
        delete(in);
    }

    public static void copy(File in, File out) throws IOException {
        if (in.exists() && in != null && out != null) {
            if (!out.exists()) {
                if (in.isDirectory()) {
                    out.mkdirs();
                } else {
                    out.createNewFile();
                }
            }
            String source = in.isDirectory() ? "directory" : "file";
            String target = out.isDirectory() ? "directory" : "file";
            if (!source.equals(target)) {
                throw new IOException("Can't duplicate " + source + " as " + target);
            } else {
                if (source.equals("directory")) {
                    File[] files = in.listFiles();
                    for (File file : files) {
                        copy(file, new File(out, file.getName()));
                    }
                } else {
                    FileChannel inCh = new FileInputStream(in).getChannel();
                    FileChannel outCh = new FileOutputStream(out).getChannel();
                    inCh.transferTo(0, inCh.size(), outCh);
                }
            }
        }
    }

    public static void delete(File file) {
        if (file != null && file.exists()) {
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (File f : files) {
                    delete(f);
                }
            }
            file.delete();
        }
    }
}

Related

  1. move(File from, File to)
  2. move(File from, File to)
  3. move(File source, File destination)
  4. move(final File from, final File to, final boolean replace)
  5. move(String sourceFile, String targetFile)
  6. moveFile(File from, File to)