Description
Move a file from one location to another.
License
Open Source License
Parameter
Parameter | Description |
---|
from | file which should be moved. |
to | desired destination of the file. |
overwrite | If false, an exception will be thrown rather than overwrite a file. |
Exception
Parameter | Description |
---|
IOException | if an error occurs. |
Declaration
public static void move(File from, File to, boolean overwrite) throws IOException
Method Source Code
//package com.java2s;
/*//from w w w . j ava 2 s . c o m
* Static File routines.
* Copyright (C) 2014-2016 Tolga Yilmaz
* info@64bitlabs.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* See LICENSE.txt for details.
*/
import java.io.*;
import java.text.MessageFormat;
import java.util.ResourceBundle;
import java.util.Locale;
public class Main {
/**
* Locale specific strings displayed to the user.
*
* @since 64bitlabsutils 1.0.0
*/
protected static ResourceBundle labels = ResourceBundle.getBundle("FileHelper", Locale.getDefault());
/**
* Buffer size when reading from input stream.
*
* @since 64bitlabsutils 1.0.0
*/
private final static int BUFFER_SIZE = 1024;
/**
* Move a file from one location to another. An attempt is made to rename
* the file and if that fails, the file is copied and the old file deleted.
*
* If the destination file already exists, an exception will be thrown.
*
* @param from file which should be moved.
* @param to desired destination of the file.
* @throws IOException if an error occurs.
*
* @since 64bitlabsutils 1.0.0
*/
public static void move(File from, File to) throws IOException {
move(from, to, false);
}
/**
* Move a file from one location to another. An attempt is made to rename
* the file and if that fails, the file is copied and the old file deleted.
*
* @param from file which should be moved.
* @param to desired destination of the file.
* @param overwrite If false, an exception will be thrown rather than overwrite a file.
* @throws IOException if an error occurs.
*
* @since 64bitlabsutils 1.0.0
*/
public static void move(File from, File to, boolean overwrite) throws IOException {
if (to.exists()) {
if (overwrite) {
if (!to.delete()) {
throw new IOException(MessageFormat.format(labels.getString("delete" + "error"),
(Object[]) new String[] { to.toString() }));
}
} else {
throw new IOException(MessageFormat.format(labels.getString("already" + "exists" + "error"),
(Object[]) new String[] { to.toString() }));
}
}
if (from.renameTo(to))
return;
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(from);
out = new FileOutputStream(to);
copy(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
if (!from.delete()) {
throw new IOException(MessageFormat.format(labels.getString("delete" + "original" + "error"),
(Object[]) new String[] { from.toString(), to.toString() }));
}
} finally {
if (in != null) {
in.close();
in = null;
}
if (out != null) {
out.flush();
out.close();
out = null;
}
}
}
/**
* Copy the data from the input stream to the output stream.
*
* @param in data source
* @param out data destination
* @throws IOException in an input or output error occurs
*
* @since 64bitlabsutils 1.0.0
*/
private static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
}
Related
- moveTo(File file, String distDir)