Java UTF8 moveFile(File inFile, File outFile)

Here you can find the source of moveFile(File inFile, File outFile)

Description

Moves a file.

License

Open Source License

Parameter

Parameter Description
inFile the file to move
outFile the destination file

Exception

Parameter Description
IOException an exception

Declaration

public static void moveFile(File inFile, File outFile) throws IOException 

Method Source Code


//package com.java2s;
/*//from   ww  w. ja va  2s.  c o m
 * org.daisy.util (C) 2005-2008 Daisy Consortium
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library 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. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

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

import java.nio.channels.FileChannel;

public class Main {
    /**
     * Moves a file.
     * @param inFile the file to move
     * @param outFile the destination file
     * @throws IOException
     */
    public static void moveFile(File inFile, File outFile) throws IOException {
        if (inFile.equals(outFile)) {
            return;
        }
        if (!inFile.isFile()) {
            throw new IOException(inFile.getAbsolutePath() + " is not a file");
        }
        if (outFile.exists()) {
            if (!outFile.isFile()) {
                throw new IOException(outFile.getAbsolutePath() + " is not a file");
            }
        } else {
            createDirectory(outFile.getParentFile());
        }

        // First try to use File.renameTo()
        boolean renameSuccess = inFile.renameTo(outFile);

        // If that fails, do copy + delete instead
        if (!renameSuccess) {
            copyFile(inFile, outFile, true);
            boolean deleteSuccess = inFile.delete();
            if (!deleteSuccess) {
                throw new IOException("Failed to delete source file " + inFile.getAbsolutePath());
            }
        }
    }

    /**
     * Make sure a directory exists.
     * @param dir
     * @throws IOException
     *             if the dir could not be created or if a regular file with the
     *             specified name exists.
     */
    public static File createDirectory(File dir) throws IOException {
        if (!dir.exists()) {
            boolean result = dir.mkdirs();
            if (!result) {
                throw new IOException("Could not create directory " + dir);
            }
        } else if (!dir.isDirectory()) {
            throw new IOException(dir + " already exists and is not a directory");
        }
        return dir;
    }

    /**
     * Copy a file using java.nio
     * 
     * @param inFile
     *            source file
     * @param outFile
     *            destination file
     * @throws IOException
     *             if anything bad happens.
     */

    public static void copyFile(File inFile, File outFile) throws IOException {
        copyFile(inFile, outFile, false);
    }

    /**
     * Copy a file using java.nio
     * 
     * @param inFile
     *            source file
     * @param outFile
     *            destination file
     * @param keepLastModified
     *            keep the "last modified" date from the input file
     * @throws IOException
     *             if anything bad happens.
     */

    public static void copyFile(File inFile, File outFile, boolean keepLastModified) throws IOException {
        if (inFile.equals(outFile))
            return;
        if (!inFile.isFile())
            throw new IOException(inFile.getAbsolutePath() + " is not a file");
        if (outFile.exists()) {
            if (!outFile.isFile()) {
                throw new IOException(outFile.getAbsolutePath() + " is not a file");
            }
        } else {
            createDirectory(outFile.getParentFile());
            outFile.createNewFile();
        }

        FileChannel source = null;
        FileChannel destination = null;
        try {
            source = new FileInputStream(inFile).getChannel();
            destination = new FileOutputStream(outFile).getChannel();
            destination.transferFrom(source, 0, source.size());
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
        if (keepLastModified) {
            outFile.setLastModified(inFile.lastModified());
        }
    }

    /**
     * Deletes the given file or directory. If the argument is a directory, then
     * the directory is deleted recursively.
     * 
     * @param file
     *            the file to delete
     * @return <code>true</code> if and only if the file or directory is
     *         successfully deleted; <code>false</code> otherwise
     * @throws IOException
     *             if the given file does not exist
     */
    public static boolean delete(File file) throws IOException {

        if (!file.exists())
            throw new IOException(file.getName() + " does not exist");

        if (isSymlink(file)) {
            return true;
        }

        boolean result = true;

        if (file.isDirectory()) {
            File[] children = file.listFiles();
            if (children != null) {
                for (File child : children) {
                    result &= delete(child);
                }
            }
        }
        result &= file.delete();
        return result;
    }

    /**
     * Checks whether a file is a symbolic link.
     * @param file the file to test
     * @return <code>true</code> if the file is a symbolic link, <code>false</code> otherwise
     * @throws IOException
     */
    public static boolean isSymlink(File file) throws IOException {
        File parent = file.getParentFile();
        File test = new File(parent.getCanonicalFile(), file.getName());
        return !test.getAbsolutePath().equals(test.getCanonicalPath());
    }
}

Related

  1. inputStreamToFile(final InputStream inputStream, final File outputFile)
  2. isSystemUtf8()
  3. isUtf8Supported()
  4. loadStringUTF8(InputStream in)
  5. makeUTF8()
  6. newUTF8Decoder()
  7. openFileUTF(String nom)
  8. putUTF8(byte[] data, int offset, String str)
  9. sort(String inFile, int keyIndex, String outFile)