Java File Move nui moveFile(File from, File to)

Here you can find the source of moveFile(File from, File to)

Description

move File

License

Open Source License

Declaration

public static void moveFile(File from, File to) 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.io.OutputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

public class Main {
    public static void moveFile(File from, File to) throws IOException {
        copyFile(from, to);/*from  ww  w .j av a  2 s  .  c  o  m*/
        from.delete();
    }

    public static void copyFile(File in, File out) throws IOException {
        out.getParentFile().mkdirs();
        FileOutputStream outStream = new FileOutputStream(out);
        try {
            copyFileToStream(in, outStream);
        } finally {
            outStream.close();
        }
    }

    public static void copyFileToStream(File in, OutputStream out) throws IOException {
        FileInputStream is = new FileInputStream(in);
        FileChannel inChannel = is.getChannel();
        WritableByteChannel outChannel = Channels.newChannel(out);
        try {
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } finally {
            if (is != null)
                is.close();
            if (inChannel != null)
                inChannel.close();
            if (outChannel != null)
                outChannel.close();
        }
    }
}

Related

  1. move(File from, File to)
  2. move(File in, File out)
  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 source, File destination)
  7. moveFile(File src, File dest)
  8. moveFile(final File srcFile, final File destFile)
  9. moveFile(String source, String target)