Android File Copy copy(File source, File target, boolean append)

Here you can find the source of copy(File source, File target, boolean append)

Description

Copies source to target.

License

Open Source License

Parameter

Parameter Description
source the File that will read bytes from
target the File that will write bytes to
append only relevant if target already exists: if true, then adds source's bytes to end of target, if false, then overwrites target

Exception

Parameter Description
IllegalArgumentException if source is Check#validFile not valid;target == null; source and target resolve to the same path
SecurityException if a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies access to source or target
IOException if an I/O problem occurs

Declaration

public static void copy(File source, File target, boolean append)
        throws IllegalArgumentException, IllegalStateException,
        SecurityException, IOException 

Method Source Code

/*/* w  w w.jav  a  2  s.  c  om*/
Copyright ? 2008 Brent Boyer

This program 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 3 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 Lesser GNU General Public License for more details.

You should have received a copy of the Lesser GNU General Public License along with this program (see the license directory in this project).  If not, see <http://www.gnu.org/licenses/>.
 */

import bb.science.FormatUtil;
import bb.util.Check;
import bb.util.StringUtil;
import bb.util.ThrowableUtil;
import bb.util.logging.LogUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.util.Random;
import java.util.logging.Level;
import org.junit.Assert;
import org.junit.Test;

public class Main{
    /**
     * Copies source to target.
     * <p>
     * @param source the File that will read bytes from
     * @param target the File that will write bytes to
     * @param append only relevant if target already exists: if true, then adds source's bytes to end of target,
     * if false, then overwrites target
     * @throws IllegalArgumentException if source is {@link Check#validFile not valid};
     * target == null; source and target resolve to the same path
     * @throws SecurityException if a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies access to source or target
     * @throws IOException if an I/O problem occurs
     */
    public static void copy(File source, File target, boolean append)
            throws IllegalArgumentException, IllegalStateException,
            SecurityException, IOException {
        Check.arg().validFile(source);
        Check.arg().notNull(target);
        if (source.getCanonicalPath().equals(target.getCanonicalPath()))
            throw new IllegalArgumentException(
                    "source and target resolve to the same path = "
                            + source.getCanonicalPath());

        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(source);
            out = new FileOutputStream(target, append);
            StreamUtil.transfer(in, out);
        } finally {
            StreamUtil.close(in);
            StreamUtil.close(out);
        }
    }
}

Related

  1. copy(File inFile, File outFile)
  2. copy(File pSourceFile, File pTargetFile)
  3. copy(File sourceFile, File destinationFile)
  4. copy(File sourceFile, File targetFile)
  5. copy(File sourceFile, File targetFile, boolean overwrite)
  6. copy(File sourceFile, String destinction)