Java Path File Move mio moveFile(String pathBefore, String pathAfter)

Here you can find the source of moveFile(String pathBefore, String pathAfter)

Description

move File

License

Open Source License

Declaration

public static void moveFile(String pathBefore, String pathAfter)
            throws java.io.FileNotFoundException, java.io.IOException 

Method Source Code

//package com.java2s;
/*//from w w w  .j  a  v a 2 s.  c o m
 * PortUtil.cs
 * Copyright ? 2009-2011 kbinani
 *
 * This file is part of org.kbinani.
 *
 * org.kbinani is free software; you can redistribute it and/or
 * modify it under the terms of the BSD License.
 *
 * org.kbinani is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

import java.io.File;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    public static void moveFile(String pathBefore, String pathAfter)
            throws java.io.FileNotFoundException, java.io.IOException {
        copyFile(pathBefore, pathAfter);
        deleteFile(pathBefore);
    }

    public static void copyFile(String file1, String file2) throws FileNotFoundException, IOException {
        FileChannel sourceChannel = new FileInputStream(new File(file1)).getChannel();
        FileChannel destinationChannel = new FileOutputStream(new File(file2)).getChannel();
        sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
        sourceChannel.close();
        destinationChannel.close();
    }

    public static void deleteFile(String path) {
        new File(path).delete();
    }
}

Related

  1. moveDirectory(Path srcPath, Path destPath)
  2. moveFile(Path from, Path to)
  3. moveFile(Path source, Path target)
  4. moveFile(Path source, Path target, boolean prompt)
  5. moveFile(Path tempPath, Path defPath)
  6. moveFile(String workspacePathOld, String workspacePathNew)